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

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

问题描述

我添加了一个语音合成到我的应用程序。它的工作原理,但问题是我无法取消演讲......例如,当我浏览到另一个页面,讲话继续......所以,我呼吁CancelAll()方法来取消当前讲话,但有异常发生我不知道为什么。你知道是什么问题?



例外

 第一类型'System.Threading.Tasks.TaskCanceledException'的机会异常出现在mscorlib.ni.dll 
型'System.Threading.Tasks.TaskCanceledException的异常出现在mscorlib.ni.dll之前没有处理一个管理/本地边界
型'System.Threading.Tasks.TaskCanceledException的异常出现在mscorlib.ni.dll和管理/本地边界
方案[2576]前没有处理TaskHost.exe已退出,代码为-1(为0xffffffff)。



我的代码:

 私人SpeechSynthesizer合成=新SpeechSynthesizer(); 

保护覆盖无效OnBackKeyPress(CancelEventArgs E)
{
//我想在这里也取消,但它是相同的异常...
}

//方​​法调用,当我按下一个按钮,取消
私人无效ButtonCancelSpeech(对象发件人,EventArgs EventArgs的)
{

{
synth.CancelAll ();
}
赶上(TaskCanceledException)
{
//我在这个异常
到达}
}

私人异步无效BtnSpeech_Click(对象发件人,EventArgs五)
{
IEnumerable的< VoiceInformation>从语音的声音=以InstalledVoices.All
,其中voice.Language.Substring(0,2).Equals(LanguageApp.GetLangage2Characters())
选择的声音;
如果(voices.ElementAt(0)!= NULL)
{
//作为确定由查询设置的声音。
synth.SetVoice(voices.ElementAt(0));

等待synth.SpeakTextAsync(_place.Description);
}
}

感谢您


< DIV CLASS =h2_lin>解决方案

既然要取消您可以使用异步操作的 IAsyncAction SpeakTextAsync 而不是使用等待

 私人SpeechSynthesizer合成=新SpeechSynthesizer(); 
私人IAsyncAction任务;

私人无效ButtonCancelSpeech(对象发件人,EventArgs EventArgs的)
{

{
//取消异步任务本身
任务。取消();
}
赶上(TaskCanceledException)
{

}
}

私人无效BtnSpeech_Click(对象发件人,EventArgs的发送)
{
IEnumerable的< VoiceInformation>从语音的声音=以InstalledVoices.All
,其中voice.Language.Substring(0,2).Equals(LanguageApp.GetLangage2Characters())
选择的声音;
如果(voices.ElementAt(0)!= NULL)
{
//作为确定由查询设置的声音。
synth.SetVoice(voices.ElementAt(0));

=任务synth.SpeakTextAsync(_place.Description);
}
}


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?

The exception

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).

My code:

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

Thank you

解决方案

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天全站免登陆