API Declaration for Visual Basic (32-bit OS) Versions 4, 5, and 6. Copy and paste into a Module.
Declare Function mciSendString Lib "winmm.dll" Alias _
"mciSendStringA" (ByVal lpstrCommand As String, ByVal _
lpstrReturnString As Any, ByVal uReturnLength As Long, ByVal _
hwndCallback As Long) As Long
API Declaration for Visual Basic (16-bit OS) Versions 3 and 4. Copy and paste into a Module.
Declare Function mciSendString Lib "mmsystem" (ByVal lpstrCommand$, ByVal lpstrReturnStr As Any, ByVal wReturnLen%, ByVal hCallBack%) As Long
NOTE: Any of the following
codes below can be copied and placed
into a control such as a command button.
OPEN
We must OPEN a WAV file before we can do anything to with it.
All of the other MCI commands listed on this web page require
this open command to be used first, otherwise they won't work.
The keyword in this command is open. The type of multimedia
file we want to use is waveaudio and the alias name that we
will use to refer to the WAVE file is voice1 (Although
we can use any alias name you desire, such as "MyWav1",
"voice2", "john1", etc... , we'll use voice1 for easy
reference in the examples that follow).
i = mciSendString("open c:\sound.wav type waveaudio alias voice1", 0&, 0, 0)
PLAY
When a WAV is open, we can play it.
The keyword in this command is play. Notice that we use the alias
voice1. This is the same as typing in c:\sound.wav, but use the alias
because you can always refer to the WAV file throughout your entire
VB program without using a global variable.
i = mciSendString("play voice1", 0&, 0, 0)
Play from...
You don't have to start playing a WAV file from the beginning. You can start
anywhere within the file. This example starts at the 3000 (3000 milliseconds)
position and plays to the end.
i = mciSendString("play voice1 from 3000", 0&, 0, 0)
Play from to...
Perhaps you have a WAV file that holds the entire alphabet. You may want
to hear certain letters. You will need code to play a specific portion
or parameter of the WAV file.
i = mciSendString("play voice1 from 1000 to 2000", 0&, 0, 0)
Wait...
A WAV file can be played without any interruption from other Windows.
The wait command sets the focus entirely on playing the WAV file
until it has reached the end. It is a good idea not to use this command
on long, time-consuming files. You are virtually "locked out" of your
computer until the file completely finishes playing.
i = mciSendString("play voice1 wait", 0&, 0, 0)
Pause
While a WAV file is being played or recorded, you can pause
it. Use the resume command to resume playing or recording.
Note:
the pause command is good to use especially when using a timer control
to loop a WAV sound repeatedly. Your timer control should detect a stopped
flag and obey it by playing from zero, but when a pause flag is detected,
you can over rule the timer settings and pause the WAV.
i = mciSendString("pause voice1", 0&, 0, 0)
SEEK
You can use this
command to reposition the current location within your
WAV file.
Seek to position 2000
i = mciSendString("seek voice1 to 2000", 0&, 0, 0)
Seek to beginning of WAV
i = mciSendString("seek voice1 to start", 0&, 0, 0)
Seek to end of WAV
i = mciSendString("seek voice1 to end", 0&, 0, 0)
Resume...
When a WAV file is paused, you can resume and continue playing it.
i = mciSendString("resume voice1", 0&, 0, 0)
Stop...
While a WAV file is being played, you can stop it. Use
the play command to start playing the file again.
i = mciSendString("stop voice1", 0&, 0, 0)
TIP: You can use the code below to play a WAV file that
has been stopped. This gives the user the "rewind illusion"
that once a WAV file is stopped (much like a music CD),
it will start from the beginning when played again.
This also distinguishes the difference from the pause command.
i = mciSendString("play voice1 from 0", 0&, 0, 0)