nativescript-community / texttospeech

Text to Speech NativeScript plugin for Android & iOS :loudspeaker:
http://nativescript-community.github.io/texttospeech/
MIT License
50 stars 24 forks source link

finishedCallback not being called #14

Closed alissonbezerra closed 6 years ago

alissonbezerra commented 6 years ago

Hi, what could cause finishedCallback not being called?

Here's my code. Note: OnClickButton is a function being called by a button click.

The app speaks my text but finishedCallback is not being called and the alert isn't being shown.

tts: TNSTextToSpeech;  

constructor(){ 
    this.tts = new TNSTextToSpeech();
    this.tts.speak({ text: "" }).then(() => {
        console.log('Inicializado');
    });
}

onClickButton() {
  let speakOptions: SpeakOptions = {
        text: 'Finalizado',
        speakRate: 0.8,
        pitch: 1.0,
        volume: 1.0,
        locale: "pt-BR",
        finishedCallback: (() => {
            alert('Finished Speaking');
        })
  };
  this.tts.speak(speakOptions);
}
alissonbezerra commented 6 years ago

I guess I've found the answer by myself, seems like it is related to this: https://stackoverflow.com/questions/20296792/tts-utteranceprogresslistener-not-being-called

I had to change the speakText method in texttospeech.android.ts to this:

 if (android.os.Build.VERSION.SDK_INT >= 21) {
      // Hardcoded this value since the static field LOLLIPOP doesn't exist in Android 4.4
      /// >= Android API 21 - https://developer.android.com/reference/android/speech/tts/TextToSpeech.html#speak(java.lang.CharSequence, int, android.os.Bundle, java.lang.String)
      let params = new android.os.Bundle();
      params.putString("volume", options.volume.toString());
      this._tts.speak(options.text, queueMode, params, "UniqueID");
    } else {
      /// < Android API 21 - https://developer.android.com/reference/android/speech/tts/TextToSpeech.html#speak(java.lang.String, int, java.util.HashMap<java.lang.String, java.lang.String>)
      let hashMap = new java.util.HashMap();
      hashMap.put("volume", options.volume.toString());
      hashMap.put(android.speech.tts.TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "UniqueID");
      this._tts.speak(options.text, queueMode, hashMap);
}

Note that this line

hashMap.put(android.speech.tts.TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "UniqueID");

was added to the code.

I tested on android 4.2.2.