Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cancel speech synthesis in windows phone 8

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

like image 789
Volkan Avatar asked Dec 20 '22 07:12

Volkan


1 Answers

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);
    }
}
like image 190
keyboardP Avatar answered Jan 04 '23 19:01

keyboardP