我正在尝试使用set up a Firebase Cloud Function来访问IBM Watson Text- to -Speech。问题是将返回的音频文件写入我的Firestore数据库。
这个测试返回工作的语音列表,将响应记录到函数日志中:
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示例节点代码:
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:
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);
});
});错误消息是
TypeError: textToSpeech.synthesize(...).on(...).then is not a function如何将从Watson返回的音频文件保存到Firestore?
发布于 2018-10-30 15:06:25
这是因为synthesize方法没有返回promise。您将需要使用一个回调构造,如下所示
textToSpeech.synthesize(params, function (err, body, response) {
if (err) {
...
} else {
// body is the audio
...
}
});https://stackoverflow.com/questions/53051763
复制相似问题