Search code examples
vb.netperformancesapi

SAPI Execution Priority


I use (SAPI) object in my application. However, whenever I executes its function, the voice runs but the application's performance (everything else) gets paused until the voice finishes! I wonder if that has something to do with the priority of this Speaking-Object. Can I somehow lower it until the rest of the code executes first? o_O

Private Function Lara(ByVal script As String) As Object
    Lara = CreateObject("SAPI.spvoice")
    Lara.Voice = Lara.GetVoices.Item(1)
    Return Lara.speak(script)
End Function

Private Sub Test_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Logo.Visible = True
    Logo.Image = ResizeImage(My.Resources.Spell)
    Lara("Welcome")
End Sub

Can (Lara) say "welcome" AFTER loading the form and its Logo?

Thank you.


Thanks to Ms. Lesley Gushurst, the voice now runs according the code order (Application shows up, its logo, then the voice). The solution was to add a (com-reference) to the project named "Microsoft Speech Object Library", then importing it in the code.

Imports SpeechLib

Private Sub Test_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Logo.Visible = True
        Logo.Image = My.Resources.Spell
        Dim Lara = CreateObject("SAPI.spvoice")
        Lara.Voice = Lara.GetVoices.Item(1)
        Lara.speak("Welcome", SpeechVoiceSpeakFlags.SVSFlagsAsync) 'It's declared now!
End Sub

Solution

  • If you take a look at MSDN's documentation SpVoice Speak method, it states that

    "When synchronous speech is used in an application, the application's execution is blocked while the voice speaks, and the user is effectively locked out. This may be acceptable for simple applications, or those with no graphical user interface (GUI), but when sophisticated user interaction is intended, asynchronous speaking will generally be more appropriate."

    So what you'll probably want to do is look into calling speak with the SVSFlagsAsync being set. As it is right now your execution is being blocked.

    I'm thinking your code would look like:

    Lara.speak(script, SpeechVoiceSpeakFlags.SVSFlagsAsync)