Cancel speech synthesis in windows phone 8

前端 未结 1 1575
花落未央
花落未央 2020-12-16 08:04

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

相关标签:
1条回答
  • 2020-12-16 08:52

    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);
        }
    }
    
    0 讨论(0)
提交回复
热议问题