c# - 在windows phone 8中取消语音合成

标签 c# windows-phone-8 text-to-speech

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

异常

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 而不是使用 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);
    }
}

关于c# - 在windows phone 8中取消语音合成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15680027/

相关文章:

c# - EF Core 慢速批量插入(约 80k 行)

c# - 多次枚举的查询结果

c++ - Windows Phone 8 上的性病

android - 如何检查设备上是否安装了文本到语音(TTS)的特定语言数据?

java - 文字转语音 : Portuguese language not available?

c# - 如何在场景之间保持值(value)?

c# - Matrix4x4 相机方法的惯用手性

c# - 为什么我不能在 Windows 手机编程中使用 FindResource()?

silverlight - 如何在 Windows Phone 8 上将数据与常量字符串绑定(bind)

javascript - 在 iOS 7+ 的 UIWebView 中使用语音合成 API 设置速率、音调、音量