Jump to content

VB Text to Speech *really Simple*

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
6 replies to this topic

#1
VBnet

VBnet

    Newbie

  • Members
  • PipPip
  • 18 posts
Hello, for this tutorial, you´ll need

1 textbox
2 command buttons/buttons

click on the first button, rename it to something cool like "Speech". then double click it and add the code :
Dim SAPI
        SAPI = CreateObject("SAPI.spvoice")
        SAPI.Speak(TextBox1.Text)
Now, click on the second button, rename it to Exit/Cancel, whatever you want.
Then double click it and add the code
End
Now put a textbox right above the 2 buttons and you are done!!

Edited by VBnet, 07 May 2009 - 09:37 AM.
add code tags (the # button)


#2
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
I wouldn't put a textbox over the buttons. I'd put it above them. ;)

Bear in mind that this code isn't very good - when late binding an object, you really need exception handling, in the event that the object cannot be found.
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#3
VBnet

VBnet

    Newbie

  • Members
  • PipPip
  • 18 posts

Xav said:

I wouldn't put a textbox over the buttons. I'd put it above them. ;)

Bear in mind that this code isn't very good - when late binding an object, you really need exception handling, in the event that the object cannot be found.

as i said *really Simple*, it is simple, not the best but working and simple, and sorry for the grammar misstype..:mad:

#4
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
Even a simple code snippet should be good enough to use in a real program. :)
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#5
VBnet

VBnet

    Newbie

  • Members
  • PipPip
  • 18 posts

Xav said:

Even a simple code snippet should be good enough to use in a real program. :)
:rolleyes:

#6
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
Go on, update the code. ;)

Dim SAPI
    Try
        SAPI = CreateObject("SAPI.spvoice")
        SAPI.Speak(TextBox1.Text)
   Catch exc As Exception
        MessageBox.Show("An error has occurred: " & exc.Message)
   Finally
        If SAPI <> Nothing Then SAPI.Dispose()
   End Try

Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#7
Sharper_Software

Sharper_Software

    Newbie

  • Members
  • PipPip
  • 17 posts

Xav said:

Go on, update the code. ;)

Dim SAPI
    Try
        SAPI = CreateObject("SAPI.spvoice")
        SAPI.Speak(TextBox1.Text)
   Catch exc As Exception
        MessageBox.Show("An error has occurred: " & exc.Message)
   Finally
        If SAPI <> Nothing Then SAPI.Dispose()
   End Try

Personally if i was making a software to read values to a customer or debug reports i would create a separate process and put this code in:
Create a new Program called (ssne.exe) and put the below code in, run, copy the \debug\ssne.exe to your main applications debug folder.
 Me.Visible = False
        Try
            Dim saynow As String
            saynow = My.Computer.FileSystem.ReadAllText(Application.StartupPath.ToString & "\saynowtmp.txt")
            Dim SAPI
            SAPI = CreateObject("SAPI.spvoice")
            SAPI.Speak(saynow)
        Catch ex As Exception
'if an error occurs just quit
            End
        End Try
'end the application when its done
        End

Then the speech would load from a different process. In the Main program i wanted to read from i would put a sub in to do this:

Dim Saynow as string
Saynow = 'What ever you want to say
'Write the speech script and overwrite the current or last script i gave it.
My.Computer.FileSystem.WriteAllText(Application.StartupPath.ToString & "\saynowtmp.txt", saynow, False)
'start the application in my folder to read what i wrote.
        Process.Start(Application.StartupPath.ToString & "\ssne.exe")

This way its releases the strain from your main program because when you invoke any SAPI it will halt the update of a programs gui and controls until it has completed its cycle.
A more professonal way of doing it is to create a multi-thread application and cast it to a seprate thread, but this would suit a simpler app that just needed to get some read.