我正在尝试在我的node.js应用中使用AWS Polly进行语音合成。当我第一次运行speak(text)
函数时一切正常但是如果我再次运行它会出现这个错误:
events.js:160
throw er; // Unhandled 'error' event
^
Error: write after end
at writeAfterEnd (/Users/noahchalifour/Desktop/HomeServer/Jarvis-Node/node_modules/readable-stream/lib/_stream_writable.js:229:12)
at Speaker.Writable.write (/Users/noahchalifour/Desktop/HomeServer/Jarvis-Node/node_modules/readable-stream/lib/_stream_writable.js:269:20)
at PassThrough.ondata (_stream_readable.js:555:20)
at emitOne (events.js:96:13)
at PassThrough.emit (events.js:188:7)
at PassThrough.Readable.read (_stream_readable.js:381:10)
at flow (_stream_readable.js:761:34)
at resume_ (_stream_readable.js:743:3)
at _combinedTickCallback (internal/process/next_tick.js:80:11)
at process._tickDomainCallback (internal/process/next_tick.js:128:9)
以下是我尝试运行的代码:
// AWS Polly
// Load the SDK
const AWS = require('aws-sdk')
const Stream = require('stream')
const Speaker = require('speaker')
// Create an Polly client
const Polly = new AWS.Polly({
signatureVersion: 'v4',
region: 'us-east-1'
})
// Create the Speaker instance
const Player = new Speaker({
channels: 1,
bitDepth: 16,
sampleRate: 16000
})
function speak(text) {
let params = {
'Text': text,
'OutputFormat': 'pcm',
'VoiceId': 'Brian'
}
Polly.synthesizeSpeech(params, (err, data) => {
if (err) {
console.log(err.code)
console.log('error');
} else if (data) {
if (data.AudioStream instanceof Buffer) {
// Initiate the source
var bufferStream = new Stream.PassThrough()
// convert AudioStream into a readable stream
bufferStream.end(data.AudioStream)
// Pipe into Player
bufferStream.pipe(Player)
}
}
})
}
speak("Hello World!")
setTimeout(function() {
speak("This is so cool!") // error is here
}, 10000)
修改
重新排序.pipe()
和.end()
之后是新错误:
request.js:31
throw err;
^
Error: write after end
at writeAfterEnd (/Users/noahchalifour/Desktop/HomeServer/Jarvis-
Node/node_modules/readable-stream/lib/_stream_writable.js:229:12)
at Speaker.Writable.write (/Users/noahchalifour/Desktop/HomeServer/Jarvis-Node/node_modules/readable-stream/lib/_stream_writable.js:269:20)
at PassThrough.ondata (_stream_readable.js:555:20)
at emitOne (events.js:96:13)
at PassThrough.emit (events.js:188:7)
at readableAddChunk (_stream_readable.js:176:18)
at PassThrough.Readable.push (_stream_readable.js:134:10)
at PassThrough.Transform.push (_stream_transform.js:128:32)
at afterTransform (_stream_transform.js:77:12)
at TransformState.afterTransform (_stream_transform.js:54:12)
答案 0 :(得分:1)
如前所述,每次在synthesizeSpeech处理程序中尝试创建Player对象。它应该解决这个问题。
更改此行,
// Pipe into Player
bufferStream.pipe(new Speaker({
channels: 1,
bitDepth: 16,
sampleRate: 16000
}))