如何在 Ionic 2 中停止 TextToSpeech

How to stop TextToSpeech in Ionic 2

我无法停止 Ionic TextToSpeech 的播放。 这是我调用的函数:

...
speaking : boolean = false;
...

sayText(txt:string, loc: string){

  if (this.speaking){
     this.tts.stop();  // <<< does not seem to work?
     this.speaking = false;
     return;
  }
  this.speaking = true;
  this.tts.speak( {text: txt, locale: loc} )
    .then((val) => { this.speaking = false;  },
          (reject) => {console.warn(reject); this.speaking = false; })
    .catch((err) => {console.error(err); this.speaking = false; });
}

这可以很好地启动语音,但是如果在播放期间再次调用它,它不会停止播放...

我在构造函数中注入了tts: TextToSpeech,当然在开头导入了声明:

import { TextToSpeech } from '@ionic-native/text-to-speech';

我错过了什么吗? 谢谢

使用空字符串调用 tts.speak 中断。

sayText(txt:string, loc: string){
  if(this.speaking){
    this.tts.speak({text: ''});  // <<< speak an empty string to interrupt.
    this.speaking = false;
    return;
  }
  this.speaking = true;
  this.tts.speak( {text: txt, locale: loc} )
 .then((val) => { this.speaking = false;  },
 (reject) => {console.warn(reject); this.speaking = false; })
 .catch((err) => {console.error(err); this.speaking = false; });
}