如何在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 ,当然在beginnning中导入了声明:
import { TextToSpeech } from '@ionic-native/text-to-speech';我错过了什么吗? 谢谢。
I am having trouble stopping the playback of Ionic TextToSpeech. Here is the function that I am calling:
... 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; }); }This starts the speech just fine, but if it's called again during the playback it does not stop the playback...
I have injected tts: TextToSpeech in constructor, and of course imported the declaration at the beginnning:
import { TextToSpeech } from '@ionic-native/text-to-speech';Am I missing something? Thanks.
最满意答案
用空字符串调用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; }); }call tts.speak with an empty string to interrupt.
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; }); }更多推荐
发布评论