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?
You need to be looking at the SpeakCompleted Event:
From Link:
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.