如何在 Ionic 2 中停止 TextToSpeech



我在停止播放 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.talk来中断。

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; });
}

相关内容

  • 没有找到相关文章

最新更新