none
How to load Image and start TTS at the same time on button click RRS feed

  • Question

  • Hello all,

    I am new to visual basic. I'm learning in Visual Studio 2015.

    On a windows form I am trying to use text to speech and want an picture (talking) to show when the "speak" button is clicked.

    At the moment the image loads after the words are spoken. Is there a way to get an image to open first (after clicking button?)  here is how I have it now...

    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim sapi
            sapi = CreateObject("sapi.spvoice")
            If (RichTextBox1.Text = "") Then
                MessageBox.Show("no text was entered")
            Else
                PictureBox1.Image = My.Resources.talking
                sapi.speak(RichTextBox1.Text)
            End If
        End Sub
    End Class

    Any help will be most appreciated

    Tuesday, March 7, 2017 12:49 PM

Answers

  • Hi ddotz,

    Firstly, you could need to right-click on the Reference, click on Add Reference, then select the System.Speech Assembly and click on OK. And then System.Speech (the System.Speech namespace contains types that support speech recognition) and System.Speech.Synthesis namespace (the System.Speech.Synthesis namespace contains classes for initializing and configuring a speech synthesis engine and for generating speech and so on).

    I put RichTextBox, Button and pictureBox controls in the Form, here is then simple sample you can refer to.

    Code Sample:

    Private speechSynthesizerObj As SpeechSynthesizer
    
        Private Sub button1_Click(sender As Object, e As EventArgs)
    
            pictureBox1.Image = Image.FromFile("D:\C# and VB Support\WinFormC\WinFormC\7.png")
    
    
    
            speechSynthesizerObj.Dispose()
    
            If richTextBox1.Text <> "" Then
    
                speechSynthesizerObj = New SpeechSynthesizer()
    
               speechSynthesizerObj.SpeakAsync(richTextBox1.Text)
    
            End If
    
        End Sub
    
    
        Private Sub Form9_Load(sender As Object, e As EventArgs)
    
            speechSynthesizerObj = New SpeechSynthesizer()
    
        End Sub
    

    Hope it is helpful to you.

    Best Regards,

    Cherry Bu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by ddotz Wednesday, March 8, 2017 5:41 PM
    Wednesday, March 8, 2017 8:51 AM
    Moderator

All replies

  • Hi ddotz,

    Welcome to the MSDN forum.

    Refer to your description, your issue is more relates to the development of VB.NET. Since our forum is to discuss Visual Studio WPF/SL Designer, Visual Studio Guidance Automation Toolkit, Developer Documentation and Help System, and Visual Studio Editor, I will help you move to the VB Forum to seek for a more professional support for your issue, thank you for your understanding.

    Best regards,

    Sara


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, March 8, 2017 7:28 AM
  • Is there a way to get an image to open first (after clicking button?)  here is how I have it now.

    You need execute the Speak method asynchronously. To do that you must include the optional Flags parameter in the call to the Speak method with the parameter flag SVSFlagsAsync set. See:

    https://msdn.microsoft.com/en-us/library/ms723609%28v=vs.85%29.aspx

    https://msdn.microsoft.com/en-us/library/ms720892(v=vs.85).aspx

    Wednesday, March 8, 2017 7:55 AM
  • Hi ddotz,

    Firstly, you could need to right-click on the Reference, click on Add Reference, then select the System.Speech Assembly and click on OK. And then System.Speech (the System.Speech namespace contains types that support speech recognition) and System.Speech.Synthesis namespace (the System.Speech.Synthesis namespace contains classes for initializing and configuring a speech synthesis engine and for generating speech and so on).

    I put RichTextBox, Button and pictureBox controls in the Form, here is then simple sample you can refer to.

    Code Sample:

    Private speechSynthesizerObj As SpeechSynthesizer
    
        Private Sub button1_Click(sender As Object, e As EventArgs)
    
            pictureBox1.Image = Image.FromFile("D:\C# and VB Support\WinFormC\WinFormC\7.png")
    
    
    
            speechSynthesizerObj.Dispose()
    
            If richTextBox1.Text <> "" Then
    
                speechSynthesizerObj = New SpeechSynthesizer()
    
               speechSynthesizerObj.SpeakAsync(richTextBox1.Text)
    
            End If
    
        End Sub
    
    
        Private Sub Form9_Load(sender As Object, e As EventArgs)
    
            speechSynthesizerObj = New SpeechSynthesizer()
    
        End Sub
    

    Hope it is helpful to you.

    Best Regards,

    Cherry Bu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by ddotz Wednesday, March 8, 2017 5:41 PM
    Wednesday, March 8, 2017 8:51 AM
    Moderator
  • Hello and thanks!

    I added System.Speech. Do you mean I need to add System.Speech.Synthesis also? (There was no reference to that in reference manager)

    Regardless it works but I had to change "SpeechSynthesizer" to "Speech.Synthesis.SpeechSynthesizer"

    Also -  speechSynthesizerObj.Dispose() causes an null reference error "Object reference not set to an instance of an object".

    Its seems to work ok as pasted below. Only I somehow need to get the picture to change again after the Text is spoken?

    Private speechSynthesizerObj As Speech.Synthesis.SpeechSynthesizer

        Private Sub button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

            PictureBox1.Image = Image.FromFile("C:\image.jpg")

            If RichTextBox1.Text <> "" Then
                speechSynthesizerObj = New Speech.Synthesis.SpeechSynthesizer()
                speechSynthesizerObj.SpeakAsync(RichTextBox1.Text)
            End If
        End Sub

        Private Sub Form9_Load(sender As Object, e As EventArgs)
            speechSynthesizerObj = New Speech.Synthesis.SpeechSynthesizer()
        End Sub


    Wednesday, March 8, 2017 5:39 PM