在 Windows Phone 8 中取消语音合成 [英] Cancel speech synthesis in windows phone 8

查看:21
本文介绍了在 Windows Phone 8 中取消语音合成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用中添加了语音合成.它有效,但问题是我无法取消演讲...例如,当我导航到另一个页面时,演讲继续...因此,我调用 CancelAll() 方法取消当前演讲,但发生了异常我不知道为什么.你知道是什么问题吗?

I add a speech synthesis to my app. It works but the problem is I can't cancel the speech...For example, when I navigate to another page, the speech continues... So, I call CancelAll() method to cancel the current speech but an exception is occured and I don't know why. Do you know what's the problem?

异常

A first chance exception of type 'System.Threading.Tasks.TaskCanceledException' occurred in mscorlib.ni.dll
An exception of type 'System.Threading.Tasks.TaskCanceledException' occurred in mscorlib.ni.dll and wasn't handled before a managed/native boundary
An exception of type 'System.Threading.Tasks.TaskCanceledException' occurred in mscorlib.ni.dll and wasn't handled before a managed/native boundary
The program '[2576] TaskHost.exe' has exited with code -1 (0xffffffff).

我的代码:

    private SpeechSynthesizer synth = new SpeechSynthesizer();

    protected override void OnBackKeyPress(CancelEventArgs e)
    {
        //I tried to cancel also here but it's the same exception...
    }

    //method called when I press a button Cancel
    private void ButtonCancelSpeech(object sender, EventArgs eventArgs)
    {
        try
        {
            synth.CancelAll();
        }
        catch (TaskCanceledException)
        {
            //I arrive in this exception
        }
    }

    private async void BtnSpeech_Click(object sender, EventArgs e)
    {
        IEnumerable<VoiceInformation> voices = from voice in InstalledVoices.All
                                                     where voice.Language.Substring(0, 2).Equals(LanguageApp.GetLangage2Characters())
                                                     select voice;
        if (voices.ElementAt(0) != null)
        {
            // Set the voice as identified by the query.
            synth.SetVoice(voices.ElementAt(0));

            await synth.SpeakTextAsync(_place.Description);
        }
    }

谢谢

推荐答案

既然你想取消异步操作,你可以使用 SpeakTextAsync 返回的 IAsyncAction 而不是使用等待.

Since you want to cancel the async operation you can use the IAsyncAction returned from SpeakTextAsync instead of using await.

private SpeechSynthesizer synth = new SpeechSynthesizer();
private IAsyncAction task;

private void ButtonCancelSpeech(object sender, EventArgs eventArgs)
{
    try
    { 
        //cancel the async task itself
        task.Cancel();
    }
    catch (TaskCanceledException)
    {

    }
    }

private void BtnSpeech_Click(object sender, EventArgs e)
{
    IEnumerable<VoiceInformation> voices = from voice in InstalledVoices.All
                                                     where voice.Language.Substring(0, 2).Equals(LanguageApp.GetLangage2Characters())
                                                     select voice;
    if (voices.ElementAt(0) != null)
    {
        // Set the voice as identified by the query.
        synth.SetVoice(voices.ElementAt(0));

        task = synth.SpeakTextAsync(_place.Description);
    }
}

这篇关于在 Windows Phone 8 中取消语音合成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆