Question

I am using the SpeechSynthesizer() in VB.net

How can I detect when the speaker has finished speaking all its data?

I have this code:

        repetitionCounter = 0
        Do
            speaker.SpeakAsync(textIntroduction.Text)
            repetitionCounter += 1
            If repetitionCounter = repeatXNumberOfTimes Then
                MsgBox("Done")
                Exit Do
            End If
        Loop

However, as soon as the Do starts, the MsgBox comes up straight away. I think this is because it loads the .SpeakAsync command into memory and as such, the repeatXNumberOfTimes is completed straight away.

I can change the code to:

      speaker.Speak(textIntroduction.Text)

This now works. However, the form 'locks up' and I cannot then interact with the form. I have tried implementing a Application.DoEvents(), yet the form still 'locks up'.

How can I get notified when the speaker has finished its speaking without locking the form up?

Also, If I want the speaker to play some text infinite number of times, how should I implement this?

*UPDATED

Here is my code:

     Dim speaker As New SpeechSynthesizer()
     Public Event SpeakCompleted As EventHandler(Of SpeakCompletedEventArgs)
     Dim handler As EventHandler(Of SpeakCompletedEventArgs)

At the Form_Load(), I have added this code:

     AddHandler speaker.SpeakCompleted, handler

Is that how it should be done? Where do I write the code to be executed when the SpeakCompleted event is fired?

Was it helpful?

Solution

You need to be looking at the SpeakCompleted Event:

From Link:

The SpeechSynthesizer raises the SpeakCompleted event at the completion of any of the SpeakAsync() or SpeakSsmlAsync(String) methods. You can write handlers for the SpeakCompleted event using SpeakCompletedEventArgs.


You do not need to create an event, the events are already part of SpeachSynthesizer. Just create a subroutine that matches the event's signature and add it to the event like this simple Command Line example.

Imports System.Speech.Synthesis

Module Module1
    Dim speaker As New SpeechSynthesizer()
    Sub Main()

        AddHandler speaker.SpeakCompleted, AddressOf SpeachComplete
        speaker.SpeakAsync("This is a test speach to see if the speach complete event raises")

        Console.ReadLine()
    End Sub

    Private Sub SpeachComplete(sender As Object, e As SpeakCompletedEventArgs)
        MsgBox("Speach Done")
    End Sub

End Module
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top