Wyo VB Lecture Notes
Objective #1 - Use sound.
- To play a sound clip in a VB program, you should include the following declaration statement for the method PlaySound in the general
declarations area of your form (where you declare module-level variables).
Private Declare Auto Function PlaySound Lib "winmm.dll" (ByVal lpszSoundName As String, ByVal hModule As Integer, ByVal dwFlags As Integer) As Integer
You do not have to memorize or fully understand this statement but you should be aware of the need to have it in a program that plays sound in a VB program.
At the point where you want an actual sound to play, you then use the PlaySound method with a call statement like this
PlaySound("buzzer.wav", 0, 1)
The path of the actual sound file must be included as the first parameter of the method. This statement only works if the audio file buzzer.wav is located in the same folder as the .exe executable
file in your VB project. The exe file is usually stored in the bin folder. However, if you move the exe file or wish to save the sound file in another folder location, you must provide the exact file
path to the audio file in the set of double quotes. However, for good style the sound file should be located in a folder named Sounds. So it's best to use this syntax:
PlaySound(System.AppDomain.CurrentDomain.BaseDirectory() + "..\..\Sounds\buzzer.wav", 0, 1)
where the System.AppDomain.CurrentDomain.BaseDirectory() method is used. This method causes VB to "figure out" what folder (also known as a directory) stores
the exe executable file. The two dots in the path of the file mean "go up one level" in the folder hierarchy. From the perspective of the exe file you must go up one level of folders to get
to the point where you would then double-click the sounds folder and then find the actual sound file named buzzer.wav. As of VB 2008, the Debug folder is considered to be the base directory for a project
since it contains the exe file.
The PlaySound method must be spelled with a capital P and a capital S. Otherwise it won't work correctly. There are many different types of audio files that can
be used with computers. Sounds that are stored with the wav audio file format will work in VB. The filename of a wav file ends with the .wav file extension. Some other sound formats are supported
but unfortunately the popular mp3 format cannot be used with the PlaySound method.
- In order to play mp3 files, you must add a Windows Media Player object to your program.
See this
Microsoft link or http://www.devasp.net/net/articles/display/304.html for
instructions on using a Windows Media Player object. See the Player
object API for details. The main steps are to first right-click the Toolbox and select Choose Items. Select the Windows Media Player control (Msdxm.ocx) under the COM tab. When the Windows
Media Player control icon appears in the Toolbox, add an object to your form. Then use code like this in a button named btnPlay
btnPlay.Enabled = False
Application.DoEvents()
Dim filename As String = Application.StartupPath
filename = filename.Substring(0, filename.Length - 3)
AxWindowsMediaPlayer1.AutoStart = False
AxWindowsMediaPlayer1.Filename = filename & "test.wav"
AxWindowsMediaPlayer1.Play()
In the AxWindowsMediaPlayer1_EndOfStream method add the following line of code btnPlay.Enabled = True
Objective #2 - Use text to speech.
- Visual Basic will read text in a computer voice. To do so you must first add a reference to the MS Speech Object Library. Click the menu command Project/Add Reference. Next, click
the COM tab and then select the entry named "Microsoft Speech Object Library".
- Type the line of code Imports SpeechLib at the top of your form and declare a module variable with the statement
Public WithEvents vox As New SpVoice
- Then use the statement
vox.Speak("Hello how are you", SpeechVoiceSpeakFlags.SVSFPurgeBeforeSpeak)
to make the computer say "Hello how are you".
- You can learn more at http://www.codeproject.com/KB/audio-video/TTSinVBpackage.aspx or by viewing a program in the Handout folder.
- You can use Notepad to create a Visual Basic script that uses text to speech on a computer that doesn't even have Visual Basic installed. By typing the code
Dim message, sapi
message = InputBox("Enter text", "Speak This")
Set sapi = CreateObject("sapi.spvoice")
sapi.Speak message
and saving this file as "Speak.vbs" (select Save As Type: All Files). You can double-click that file to run the script.