Search code examples
javascriptvue.jsnativescripttext-to-speechnativescript-vue

How to use finished callback function in nativescript-texttospeech?


How to order nativescript texttospeech messages with a callback function?

How to use finished callback function?

nativescript-texttospeech has a finishedCallback property in SpeakOptions.

I need TTS to read texts one by one that's all.

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


Solution

  • Try wrapping your own promise, that should help

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