首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将IBM Watson Text-to-Speech与Firebase Cloud Functions结合使用?

将IBM Watson Text-to-Speech与Firebase Cloud Functions结合使用?
EN

Stack Overflow用户
提问于 2018-10-30 02:33:14
回答 1查看 380关注 0票数 0

我正在尝试使用set up a Firebase Cloud Function来访问IBM Watson Text- to -Speech。问题是将返回的音频文件写入我的Firestore数据库。

这个测试返回工作的语音列表,将响应记录到函数日志中:

代码语言:javascript
复制
exports.test = functions.firestore.document('IBM_Watson_Token/Test_Value').onUpdate((change, context) => {
  var textToSpeech = new TextToSpeechV1({
    username: 'groucho',
    password: 'swordfish'
  });

  return textToSpeech.listVoices(null, function(error, voices) {
    if (error) {
      console.log(error);
    } else {
      console.log(JSON.stringify(voices, null, 2));
    }
  });
});

以下是用于返回音频文件并将其写入服务器的documentation示例节点代码:

代码语言:javascript
复制
var TextToSpeechV1 = require('watson-developer-cloud/text-to-speech/v1');
var fs = require('fs');

var textToSpeech = new TextToSpeechV1({
  username: '{username}',
  password: '{password}'
});

var synthesizeParams = {
  text: 'Hello world',
  accept: 'audio/wav',
  voice: 'en-US_AllisonVoice'
};

// Pipe the synthesized text to a file.
textToSpeech.synthesize(synthesizeParams).on('error', function(error) {
  console.log(error);
}).pipe(fs.createWriteStream('hello_world.wav'));

Firebase不允许使用fs将文件写入服务器,您必须写入Firestore数据库。我更改了示例代码的最后一行,使用promise将其写入Firestore:

代码语言:javascript
复制
exports.test = functions.firestore.document('IBM_Watson_Token/Test_Value').onUpdate((change, context) => {
  var textToSpeech = new TextToSpeechV1({
    username: 'groucho',
    password: 'swordfish'
  });

  var synthesizeParams = {
    text: 'Hello world',
    accept: 'audio/wav',
    voice: 'en-US_AllisonVoice'
  };

    return textToSpeech.synthesize(synthesizeParams).on('error', function(error) {
    console.log(error);
  }).then(function (audiofile) {
    admin.firestore().collection('IBM_Watson_Token').doc('hello_world').set({
      'audiofile': audiofile
    })
  })
  .catch(function (error) {
    console.log(error);
  });
});

错误消息是

代码语言:javascript
复制
TypeError: textToSpeech.synthesize(...).on(...).then is not a function

如何将从Watson返回的音频文件保存到Firestore?

EN

回答 1

Stack Overflow用户

发布于 2018-10-30 15:06:25

这是因为synthesize方法没有返回promise。您将需要使用一个回调构造,如下所示

代码语言:javascript
复制
    textToSpeech.synthesize(params, function (err, body, response) {
      if (err) {
        ...
      } else {
        // body is the audio
        ... 
      }
    });
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53051763

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档