javascript - 如何在 nativescript-texttospeech 中使用完成的回调函数?
问题描述
如何使用回调函数订购 nativescript texttospeech 消息?
如何使用完成的回调函数?
nativescript-texttospeechfinishedCallback
在 SpeakOptions 中有一个属性。
我需要 TTS 来一一阅读文本。
talk("First message");
talk("Second message");
talk("Last message");
<template>
<Page>
<ActionBar title="Speak Promises Component" />
<StackLayout>
<Label :text="speakoptions.text" col="0" row="0" />
<Button text="start" @tap="start" />
</StackLayout>
</Page>
</template>
<script >
import { TNSTextToSpeech, SpeakOptions as speakoptions } from "nativescript-texttospeech";
let TTS = new TNSTextToSpeech();
export default {
data() {
return {
speakoptions: {
text: " ",
locale: "en-GB",
finishedCallback: "" // what kind of function it should be?
}
};
},
methods: {
start: async function() {
await this.talk("First message");
await this.talk("Second message");
await this.talk("Last message");
},
talk: function(message) {
this.speakoptions.text = message;
return TTS.speak(this.speakoptions);
}
}
};
</script>
<style scoped >
</style>
解决方案
尝试包装自己的承诺,这应该会有所帮助
talk: function(message) {
var speakOptions = { ...this.speakoptions, text: message };
return new Promise((resolve, reject) => {
speakOptions.finishedCallback = resolve;
TTS.speak(speakOptions)
});
}
推荐阅读
- azure - Kusto 将 ID 转换为名称
- excel - 是否有使用 vba 复制另一个工作簿的防弹方法?
- javascript - 在缩放期间在时间刻度上设置刻度
- javascript - VueJS, an endless loop of object creation and Error: "Maximum call stack size exceeded"
- c# - ML.NET 向现有模型添加新数据
- sql - 带有存储过程的 UNION
- tensorflow - 如果缺少 ckpt.index 文件,则恢复检查点
- rust - 无法将实现特征的结构装箱为特征对象
- r - 根据 R 中的多个条件改变新列
- swiftui - 从 SwiftUI 中的父视图推送多个导航链接