按以下尝试修改下TTSSpeakerUtil文件内容:
[@Concurrent](/user/Concurrent)
function writeBuf(filePath: string, buf: ArrayBuffer, offset: number = 0, seq: number) {
console.info(`Demo start:${offset}, seq: ${seq}`)
// 新建并打开文件
let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
fs.writeSync(file.fd, buf, { offset });
// 关闭文件
fs.closeSync(file);
console.info(`Demo end:${offset}, seq: ${seq}`)
}
class FileWriter {
private offset: number = 0;
private path: string = '';
private count = 0;
private group: taskpool.SequenceRunner = new taskpool.SequenceRunner();
private pcmData: TreeMap<number, ArrayBuffer> = new TreeMap();
constructor(contentId: string) {
this.path = getPlayItemPath(contentId)
}
public write(buff: ArrayBuffer, seq: number): Promise<void> {
this.pcmData.set(seq, buff);
while (this.pcmData.length > 0 && this.count == this.pcmData.getFirstKey()) {
let buf: ArrayBuffer = this.pcmData.remove(this.pcmData.getFirstKey());
let task = new taskpool.Task(writeBuf, this.path, buf, this.offset, this.count);
let length = buf.byteLength;
this.group.execute(task);
this.offset += length;
this.count++;
}
}
public delete() {
fs.unlink(this.path)
}
public close(): void {
}
canRead(): boolean {
return this.count > 100
}
}
// onData回调修改
// 返回音频流
async onData(requestId: string, audio: ArrayBuffer, response: textToSpeech.SynthesisResponse) {
ZHGLog.info("covert Listener ", 'onData: ' + 'requestId: ' + requestId + ' response: ' + JSON.stringify(response));
fileManager.get(requestId).write(audio, response.sequence);
if (needPlay && fileManager.get(requestId).canRead()) {
audioPlayerManager.playOrPause(requestId, false)
needPlay = false
}
},