如何在Ionic 2中停止TextToSpeech(How to stop TextToSpeech in Ionic 2)

编程入门 行业动态 更新时间:2024-10-27 12:34:31
如何在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; }); }

更多推荐

本文发布于:2023-08-07 12:08:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464183.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何在   Ionic   stop   TextToSpeech

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!