首页 > 解决方案 > 如何在 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>


编辑 Vue 模板

标签: javascriptvue.jsnativescripttext-to-speechnativescript-vue

解决方案


尝试包装自己的承诺,这应该会有所帮助

talk: function(message) {
  var speakOptions = { ...this.speakoptions, text: message };
  return new Promise((resolve, reject) => {
     speakOptions.finishedCallback = resolve;
     TTS.speak(speakOptions)
  });
}

推荐阅读