vb.nettext-to-speechspeechsynthesizer

SpeechSynthesizer.SpeakAsyncCancelAll() does not cancel


I have a button on my form that is labeled Speak, when user clicks on it synth.SpeakAsync(Textbox.text) is fired, and the button changes its text to "Stop Speaking".

However when a user clicks on stop speaking, thereby firing the synth.speakAsyncCancelAll() it doesn't cancel the speech mid text.

HERE IS MY CODE:

Private Sub btnSpeak_Click(sender As System.Object, e As System.EventArgs) Handles btnSpeak.Click
    If RTextBox.Text = "" Then Exit Sub

    Dim synth As New SpeechSynthesizer
    synth.Rate = -2

    If btnSpeak.Text = "Stop Speaking" Then
        btnSpeak.Text = "Speak"
        synth.SpeakAsyncCancelAll()

    ElseIf btnSpeak.Text = "Speak" Then
        btnSpeak.Text = "Stop Speaking"
        synth.SpeakAsync(RTextBox.Text)
    End If

Solution

  • Every time you click the button a new speech synthesizer is being created. Take the dim synth as new speech synthesizer out of the button click event. this way it will refer to the same synthesizer each time and it should then fire the speech cancelled event