Wyo VB Lecture Notes
Objective #1 - Play wav audio files.
- You can make sounds play in a VB program. You must first download the desired sound file in order to add it to your project. While there are many different kinds of sound files such as mp3, m4a, wav, au, and others, VB prefers wav files.
- There are websites that provide sound clips which are short sounds that are a few seconds long. Many are free for educational use and some may even be worth paying for from websites that specialize in selling professionally-produced sound clips. You can search Google for "free wav sound clips" to find your own. When you find one that you would like to use, you can right-click on the link to save the sound file to your Downloads or Music folder. Here is an example of a wav file that you may download.
- You can use the PlaySound function (i.e. method) to play a wav sound clip. This will not work for mp3 or other types of sound files.
First, you must declare a reference to the PlaySound method by adding the following statement just below Public Class Form1 like this
Public Class Form1
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 declaration statement for quizzes or tests in this VB course 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("pacmandying.wav", 0, 1)
The PlaySound method must be spelled with an uppercase P and S. It is not important that you understand what the 0 and 1 represent but you must know that the name of the sound file is enclosed in double quotes as the first parameter in the parentheses.
For example, you may want to place this call statement in an If statement that detects a collision between a player (picPlayer) and a ghost (picGhost) as in
If (picGhost.Right >= picPlayer.Left And picGhost.Left <= picPlayer.Right And picGhost.Bottom >= picPlayer.Top And picGhost.Top <= picPlayer.Bottom) Then
PlaySound("pacmandying.wav", 0, 1)
End If
- You must also place the actual sound file in the proper location in your VB project folder. I recommend that you copy and paste the wav file ( pacmandying.wav in the example above) into your project's Debug folder which is found inside the project's bin folder. This is the same location as your program's executable (.exe) file.
- 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\pacmandying.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. The Debug folder is considered to be the base directory for a project
since it contains the exe file.
- Another way to play a wav file as background music is to use the following example that would play a .wav file named pacmanintro.wav if it was stored in the project's bin folder:
My.Computer.Audio.Play("pacmanintro.wav", AudioPlayMode.Background)
Objective #2 - Play mp3 audio files.
- When you want a whole song to play in the background of a VB program, you will likely have the song as an mp3 file rather than a wav file. Here is an example of an mp3 file that you can download. The PlaySound method that is described above will not work for mp3 files.
- Interesting but not tested on any quizzes or tests....
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:
-
right-click the Toolbox and
-
select Choose Items.
- click the COM Components tab
- scroll to find the Windows Media Player control (probable path is C:\Windows\system32\wmp.dll) under the COM Components tab
- place a checkmark next to Windows Media Player
- click OK to close the window
Then, place the following code into a button's Click event or theForm1_Load method.
AxWindowsMediaPlayer1.URL = "example.mp3"
AxWindowsMediaPlayer1.Ctlcontrols.play()
- You do not need to rename the Windows Media Player object. It may keep the default name of AxWindowsMediaPlayer1
- You can set the Visible property of the Windows Media Player object to False so the user doesn't see it and cannot control the playback of the song (pause, volume, etc.)
Objective #3 - Use text to speech.
- You can program VB to read text in a computer voice similar to Siri on an iPhone.
- To do so you must first add a reference to the Microsoft Speech Object Library.
- Click the menu command Project/Add Reference.
- Next, click
the COM tab
- Then, select the entry named "Microsoft Speech Object Library" & click the OK button
- 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
That is, the top of your code window will look like this
' John Doe
' Maze3
' Period 1
Imports SpeechLib
Public Class Form1
Public WithEvents vox As New SpVoice
- Then use the Speak method with a statement like this
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.
- Interesting but not tested on any quizzes or tests....
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.