Q: How do I play MID, WAV or other multimedia files?
There are several ways to do this and you can get more information by checking out our Sound & Games tutorial. For simple sound play one of the easiest ways is to use mciExecute. You can maximize this window to view the rest of this information.
mciExecute
Description
This function takes a single argument
and executes MCI commands.
Declare Statement
#if WIN32 Then Public Declare Function mciExecute& Lib "winmm.dll" (ByVal lpstrCommand As String) #else Public Declare Function mciExecute% Lib "mmsystem.dll" (ByVal lpstrCommand As String) #endif
Return Value
Returns a long integer value that
indicates success or failure.
Comments
Takes the "Play" command and
will open, play and close the called MCI device.
Sample Code
Start a new project and place a command button on the form. Next add the declare statements to your project. Change the caption property to Play and the name to cmdPlayMCI . Double-click on the command button and add the following code;
Dummy% = mciExecute("play c:\windows\tada.wav")
That's it! Assuming you checked to be sure you actually have a tada.wav the path is correct, and you have a sound card; you should have just played a sound. Actually you can play any MCI supported file, so if you wanted to play a MIDI sequence you could do this;
Dummy% = mciExecute("play c:\windows\canyon.mid")
In case you're wondering what the Dummy% is all about...remember we said that this function returns a value indicating success or failure? Well you could use that value to do something depending on whether the program found the file or not. For the purposes of this simple example, you checked to make sure the files exist so we didn't need the returned value.
This is pretty basic and there are other issues if you want to play multiple sounds or simultaneous sounds that may not be handled this easily. Still I hope this serves to wet your appetite and you can ask other questions on the VB Forums section of this site as you progress.