[英]How to stop/cut-off xamarin essentials text-to-speech on iOS to start new speech
在启动Xamarin.Forms时,无法停止xamarin要点文本到语音转换以启动新的文本到语音“ SpeakAsync”调用。
使用此应用程序作为“练习电话”的模拟话务员,我想在用户挂断电话时切断说“ hello this is ....”的话务员,并使用语音异步开始说“这通话结束后,通话立即结束”
await TextToSpeech.SpeakAsync("Hello this is the operator, thank you for calling, how can I help you?"
, SpeechTokenSource.Token);
目前这可以在android上按预期工作
public void CancelSpeech()
{
if (SpeechTokenSource?.IsCancellationRequested ?? true)
return;
SpeechTokenSource.Cancel();
}
尝试使用文档( https://docs.microsoft.com/zh-cn/xamarin/essentials/text-to-speech )中列出的取消令牌,但是我无法获得当前的“ SpeakAsync”语音以立即停止播放iOS,或下一个讲语音同步命令,一旦取消初始的文本转语音,即可在iOS上开始讲文本。
CancelSpeech();
IsCallActive = false;
DialedText = null;
//Reset speech cancellation token
SpeechTokenSource = new CancellationTokenSource();
await TextToSpeech.SpeakAsync("This call has ended. Goodbye."
, SpeechTokenSource.Token);
在iOS输出中记录的错误:“ [[AXTTSCommon] _BeginSpeaking:无法开始播放”
我有什么想念的吗? 如何取消当前正在播放的文本转语音并在此后立即开始下一个语音?
编辑:
这只需单击一个按钮即可完成(取消当前语音并开始“此通话已结束”语音)
我无法重现您的问题。
以下代码使用Xamarin.Essentials v1.3.0和Xamarin.Forms v4.2.0.709249在iOS和Android上均可使用。
轻按“ speakButton
,将说出语音;当轻按“ cancelButton
轻按“ cancelButton
时,语音将停止。
在iOS和Android上取消SpeakAsync
时有一个区别:
完整的示例应用程序可以在这里找到: https : //github.com/brminnick/CancelTextToSpeechSample/tree/master
using System;
using System.Threading;
using Xamarin.Forms;
namespace CancelTextToSpeechSample
{
public class App : Application
{
public App()
{
MainPage = new CancelSpeakPage();
}
class CancelSpeakPage : ContentPage
{
const string _spokenText = "Oh, supercalifragilisticexpialidocious! Even though the sound of it, Is something quite atrocious, If you say it loud enough, You'll always sound precocious, Supercalifragilisticexpialidocious!";
CancellationTokenSource _speakButtonCancellationTokenSource;
public CancelSpeakPage()
{
var speakButton = new Button { Text = "Sing" };
speakButton.Clicked += HandleSpeakButtonClicked;
var cancelButton = new Button { Text = "Cancel Song" };
cancelButton.Clicked += HandleCancelButtonClicked;
Content = new StackLayout
{
VerticalOptions = LayoutOptions.Center,
HorizontalOptions = LayoutOptions.Center,
Children =
{
speakButton,
cancelButton
}
};
}
void HandleCancelButtonClicked(object sender, EventArgs e)
{
_speakButtonCancellationTokenSource?.Cancel();
}
async void HandleSpeakButtonClicked(object sender, EventArgs e)
{
if (_speakButtonCancellationTokenSource?.IsCancellationRequested is false)
_speakButtonCancellationTokenSource.Cancel();
_speakButtonCancellationTokenSource = new CancellationTokenSource();
await Xamarin.Essentials.TextToSpeech.SpeakAsync(_spokenText, _speakButtonCancellationTokenSource.Token);
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.