Search code examples
vb.nettext-to-speechvb.net-2010sapiwindows-media-player

Play MediaPlayer sound first before SAPI text to speech VB.Net


We Created A Queuing System Using Vb.net 2015 and SQL SERVER 2012

We want to program to play a ding dong sound first (to notify the waiting queue) then the text to speech to read the number and counter serving it. However, the output becomes the opposite. It plays text to speech [SAPI] first before playing the ding dong sound. I also noticed that when SAPI is "reading" the text, the time freezes while it is reading it.

Here is my Code:

 'play ding dong sound
    Dim fileLoc As String = Application.StartupPath & "\" & "ding dong queue.mp3" '"Reminder.wav" '

    Me.AxWindowsMediaPlayer1.URL = fileLoc

    If (Me.AxWindowsMediaPlayer1.playState = WMPLib.WMPPlayState.wmppsPlaying) Then

        'playStateLabel.Text = "Windows Media Player is playing!"

    Else
        Dim tempString As String = ""

        For Each element As Char In lblTicketNoShow.Text 'seperate each number for tts ex. "1 0 0 1"
            tempString = tempString & element & vbTab
        Next


        Dim tts = CreateObject("SAPI.spvoice")
        tts.rate = -4
        tts.volume = 100
        tts.speak("NOW SERVING " & tempString & " on Counter " & lblCounterNo.Text)


    End If

What was the problem with my code. Am I missing something?


Solution

  • Offhand, I'd say that you're not waiting for the ding sound to finish playing.

    As it's written, you're setting the URL on the media player object and then immediately telling SAPI to speak. This is a race condition, and apparently SAPI is a bit faster off the block, resulting in the behavior you're seeing.

    You'll need a PlayStateChange event handler on AxWindowsMediaPlayer1; when you get a MediaEnded event from the PlayStateChange handler, you can tell SAPI to read the next event. (You'll probably want a SAPI event handler to tell when SAPI's done, too.)