Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
 
TitlePlay a .wav audio file using the MCI API
DescriptionThis example shows how to play a .wav audio file using the MCI API in Visual Basic 6. The program uses the mciSendString and mciGetErrorString API functions.
KeywordsMCI, sound, wave, waveaudio
CategoriesMultimedia
 
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
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated