Many Windows applications use sound to communicate with users. Some applications play a sound to indicate when a specific event occurs, such as when the application has finished loading or has run into a problem. Visual Basic 2005 Express provides support for playing sounds in the form of wave files.
There are plenty of different sources of wave files at your disposal. For example, you can download an untold number of wave files from the Internet. You will also find an assortment of wave files on your computer. Windows XP, for example, stores a number of wave files in the C:\Windows\Media folder.
A wave file is a digital audio file with a .wav file extension that was originally developed by Microsoft. Wave files store uncompressed raw audio data and are supported by all Windows operating systems as well as by non-Windows platforms such as the Macintosh.
Trick | If you cannot find a wave file that provides the sound you want played by your application, you can always make your own custom wave file using the Sound Recorder utility provided with Windows. To access the Sound Recorder, click on Start, All Programs, Accessories, Entertainment, and then Sound Recorder. The Sound Recorder utility will appear as shown in Figure 10.10. |
You can play a wave file within your applications using the SoundPlayer class. You can programmatically create a new SoundPlayer object, as demonstrated below.
Dim MySound As New SoundPlayer
Once instantiated, you can use the MySound variable to access properties and methods belonging to the SoundPlayer class, including those listed below.
SoundLocation. A SoundPlayer property that is used to specify the location of the wave file to be played
Play(). A SoundPlayer method that plays the wave file specified by the SoundPlayer class's SoundLocation property
PlayLooping(). A SoundPlayer method that repeatedly plays the wave file specified by the SoundPlayer class's SoundLocation property
Stop (). A SoundPlayer method that stops playing any currently executing sound
The following example provides a demonstration of how to instantiate an object based on the SoundPlayer class and then use it to repeatedly play a wave file when the application loads.
'Import the System.Media namespaces Imports System.Media Public Class frmMain 'Declare and instantiate the SoundPlayer Private MySound As New SoundPlayer Private Sub frmMain_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load 'Specify the location of the wave file MySound.SoundLocation = "C:\Windows\Media\chimes.wav" MySound.PlayLooping() 'Repeatedly play the sound file End Sub End Class
Hint | Note the inclusion of the Imports System.Media statement at the beginning of this example. This statement tells Visual Basic to include a reference to the System.Media namespace, which is required in order to get access to the SoundPlayer Class. |
One of the few real differences between Visual Basic 2003 and Visual Basic 2005 is the expansion of the .NET Framework's class library, which includes a number of new classes. One collection of classes in particular that is of special interest is the collection that makes up the new My namespace. The My namespace provides access to a number of classes that can be used to instantiate objects that greatly facilitate rapid application development. These classes include:
My.Application. Gets information about the currently running application, such as its name and version
My.Computer. Gets information about the local computer, including its hardware
My.User. Gets information about the current user, including username and domain name
My.Forms. Gets information regarding any currently open application forms
You can use properties and methods belonging to the My.Computer class to quickly and easily add the ability to play wave audio files in your Visual Basic applications. To demonstrate this, let's develop a new application called the Audio Jukebox. This application will use properties and methods belonging to the My.Computer class to play .wav files. Specifically, it will use the Play method belonging to the Audio class, which is a subclass of the My.Computer class, to play a wave file. It will also use the FileSystem subclass's GetFiles method to generate a list of wave files and the FileSystem class's CurrentDirectory property to specify the location of these wave files. Figure 10.11 shows what the application will look like when you have finished building it.
Figure 10.11: The Audio Jukebox application demonstrates how to control the play of various wave files.
The application's user interface is made up of a form with a ListBox and Button control. Assign a name of lbxwaveFiles to the ListBox control and a name of If btnPlay to the Button control. Then add the following code to the application as shown below.
A ListBox control is used to display a list of items from which a selection can be made.
Trick | One way to populate a ListBox control with a list of items is to use the ListBox class's DataSource property. Once populated, you can use the ListBox class's SelectedItem property to determine which item is selected by the user. The audio Jukebox application demonstrates how to use the DataSource property to display a list of wave files and the SelectedItem property to determine which wave file is selected for play. |
Public Class frmMain Private Sub frmMain_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load 'Specify the location of the wave files to be displayed by the 'application My.Computer.FileSystem.CurrentDirectory = "C:\Windows\Media" 'Load a list of wave files into the ListBox control lbxWaveFiles.DataSource = My.Computer.FileSystem.GetFiles( _ My.Computer.FileSystem.CurrentDirectory, _ FileIO.SearchOption.SearchTopLevelOnly, "*.wav") End Sub Private Sub btnPlay_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnPlay.Click 'Play the selected wave file My.Computer.Audio.Play(lbxWaveFiles.SelectedItem.ToString) End Sub End Class
The first statement requires a little explanation. It begins with the My class. This class provides access to the Computer class. The Computer class has a property named FileSystem, which provides access to the FileSystem class, which in turn has a properly named CurrentDirectory, which is set equal to "C:\Windows\Media".
The second statement populates the ListBox control with a list of wave files located in the "C:\Windows\Media" folder. This statement also requires some additional explanation. This statement works by executing the My.Computer.FileSystem.GetFiles method, which is passed three arguments in the following order: target folder, a value specifying whether subfolders should be recursively displayed (FileIo.SearchOption.SearchTopLevelOnly), and a string specifying the files to be retrieved. In the case of this example, all files with a .wav file extension are displayed.
In order to use the application, the user must select one of the wave files displayed in the ListBox control and then click on the Button control labeled Play, which then uses the Audio class's Play method to play the selected wave file.
As you can see, the My namespace and the objects that can be generated from its classes provide you with the ability to perform complex tasks with very little coding and offer yet another alternative for adding sound to your Visual Basic applications.