如何在Ionic 2中停止TextToSpeech

时间:2017-07-22 16:48:34

标签: ionic2 text-to-speech

我无法停止播放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';

我错过了什么吗? 感谢。

1 个答案:

答案 0 :(得分:1)

用空字符串调用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; });
}