The objective of this tutorial is to learn how to incorporate basic sound effects into VB applications. At the end of this tutorial you should be able to:
Locate and add the MMControl VBX to a Visual Basic project.
Add a MMControl to a form and control it through code.
Play simple .WAV files included with Windows or Windows 95.
Before incorporating any sound into a VB application, the MMControl must be loaded into the toolbox. If it is not there already there are two ways to obtain it: 1) start a new project making sure to select VB Professional Edition Controls, or 2) if working with an existing project goto Project/Components..., look down the list for Microsoft Multimedia Control 5.0 and select it.
Select the MMControl from the toolbox and place it on a form (a short wide rectangle). Now go to the Properties window and set PlayEnabled, PlayVisible, StopEnabled, StopVisible, PauseEnabled, PauseVisible, PrevEnabled and PrevVisible all to True. For the current example we will not need any of the other settings on the MMControl. Your control should now look like:
Assign the MMControl a suitable name and enter as the Filename C:\WINDOWS\MEDIA\CHORD.WAV. Also, set the DeviceType to WaveAudio.
Even though we have now assigned the control a filename to play (one of the WAV files which comes as standard with Windows), we still need to do a little more before we run the program. The first thing is to double click on the form to bring up a code window. Then in the Load event enter the following code:
Private Sub Form_Load () MMControl1.Notify = False MMControl1.Wait = False MMControl1.Shareable = False MMControl1.Command = "Open" End Sub
An extra line in the form Unload event is also needed:
Private Sub Form_Unload (Cancel As Integer) MMControl1.Command = "Close" End Sub
Try running the program now. To actually hear the .WAV file click on the play button. The Stop and Pause buttons work in a similar way to a tape recorder. Remember that after hearing a file you have to rewind it before you play it again.
Note: If you want to play .MID files you must change the DeviceType of the MMControl to Sequencer. Or if you want to play Audio CD tracks then you must change the DeviceType to CDAudio.