Search code examples
c#speech-recognitiontext-to-speechspeechspeech-to-text

How to Stop SpeakAsync if another message were about to speak out?


I'm trying to create a web page with speech recognition using c# as my code behind. Please help me because I want to know how may I stop a SpeakAsync to deliver the full message when another message started to speak.

pseudocode like this

 protected void Button1_Click(object sender, EventArgs e)
    {
        ss.SpeakAsync(CurrentStop); // it means if there is a current message it should stop before delivering the next line
        ss.SpeakAsync("Phrase and Sentence");
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        ss.SpeakAsync(CurrentStop);
        ss.SpeakAsync("Phrase is a group of words without a complete thought.");
    }

please help me. It's my first time to use c# as well as system.speech


Solution

  • Use the GetCurrentlySpokenPrompt and SpeakAsyncCancel methods to cancel the current prompt:

    var current = ss.GetCurrentlySpokenPrompt();
    
    if (current != null)
        ss.SpeakAsyncCancel(current);
    

    Full Example

    var ss = new SpeechSynthesizer();
    
    ss.SpeakAsync("This is a test.");
    
    Thread.Sleep(300);
    
    var current = ss.GetCurrentlySpokenPrompt();
    
    if (current != null)
        ss.SpeakAsyncCancel(current);
    
    ss.SpeakAsync("Goodbye.");