如何使用回调函数订购 NativeScript texttospeech 消息?
如何使用完成回调函数?
nativescript-texttospeech 在 SpeakOptions 中有一个 finishedCallback
属性。
我需要 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>
最佳答案
尝试包装你自己的 promise ,这应该会有帮助
talk: function(message) {
var speakOptions = { ...this.speakoptions, text: message };
return new Promise((resolve, reject) => {
speakOptions.finishedCallback = resolve;
TTS.speak(speakOptions)
});
}
关于javascript - 如何在nativescript-texttospeech中使用完成的回调函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58936344/