javascript - 如何在nativescript-texttospeech中使用完成的回调函数?

标签 javascript vue.js nativescript text-to-speech nativescript-vue

如何使用回调函数订购 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>


Edit Vue Template

最佳答案

尝试包装你自己的 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/

相关文章:

javascript - 脚本注入(inject) Joomla 模板

javascript - AngularJS x-可编辑: Accept numeric input only

android - 运行时找不到兼容的Android SDK进行编译 `tns platform add android`

javascript - 在函数中调用输入类型

javascript - 名称为 'mainController' 的 Controller 未注册

javascript - 使用 Vue.JS/Axios 和来自第三方 API(基于 Django!)的数据填充 DOM

javascript - vue.js :selected doesn't triggering @change event select/option

vue.js - 在没有父包装器的情况下使用数据变量作为 Vue 模板

javascript - Nativescript ListView [object 对象]

android - 在 NativeScript 应用程序上集成 Android 服务