This example shows how to play a .wav audio file using the MCI API

Started by nandagopal, Dec 19, 2008, 05:27 PM

Previous topic - Next topic

nandagopal

The program composes a command string of the form:

    open waveaudio!file alias myaudio wait

Where file is the path to the .wav file.

It sends this command to the mciSendString function. It then sends the command "Play myaudio wait" to play the file and "close myaudio wait" to close the file when it is done.


Private Sub cmdPlay_Click()
Dim cmd As String
Dim ret As Long
Dim buf As String
Dim pos As Integer

    ' Open the file.
    cmd = "open waveaudio!" & _
        txtFile.Text & " alias myaudio wait"
    ret = mciSendString(cmd, vbNullString, 0, 0)
    If ret <> 0 Then
        buf = Space$(1024)
        mciGetErrorString ret, buf, Len(buf)
        pos = InStr(buf, Chr$(0))
        buf = Left$(buf, pos - 1)
        MsgBox buf
        Exit Sub
    End If

    ' Play the file.
    cmd = "Play myaudio wait"
    ret = mciSendString(cmd, vbNullString, 0, 0)
    If ret <> 0 Then
        buf = Space$(1024)
        mciGetErrorString ret, buf, Len(buf)
        pos = InStr(buf, Chr$(0))
        buf = Left$(buf, pos - 1)
        MsgBox buf
        Exit Sub
    End If

    ' Close the file.
    cmd = "close myaudio wait"
    ret = mciSendString(cmd, vbNullString, 0, 0)
    If ret <> 0 Then
        buf = Space$(1024)
        mciGetErrorString ret, buf, Len(buf)
        pos = InStr(buf, Chr$(0))
        buf = Left$(buf, pos - 1)
        MsgBox buf
        Exit Sub
    End If
End Sub