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 an MP3 audio file
Keywordsmultimedia, MP3, audio, sound
CategoriesMultimedia, Graphics
 
Use the MMC (multimedia control). The control's Command property makes the control do things. The control supports the following commands:

CommandPurpose
OpenOpens the MP3 file
PlayPlays the open MP3 file
StopStops playing the MP3 file
CloseCloses the MP3 file

This program's cmdPlay button opens the indicated file and plays it. It then changes the button's Caption to Stop. When the user clicks the button again, the program stops playing the file.

The MMC's StatusUpdate event fires regularly so the program can display status. In this case, the program displays the control's position in the file.

The MMC's Done event fires when the file is done playing, either because the control reached the end of the file or because the user clicked the Stop button and the program set the MMC's Command property to Stop. The event handler closes the MP3 file and resets the button's caption.

The form's Unload event handler closes the MP3 file in case it is still open.

 
Private Sub Form_Load()
Dim app_path As String

    app_path = App.Path
    If Right$(app_path, 1) <> "\" Then app_path = app_path _
        & "\"
    txtFile.Text = app_path & "Alice_Cooper-Gimmie.mp3"

    ' Prepare the audio control.
    mmcAudio.Notify = False
    mmcAudio.Wait = True
    mmcAudio.Shareable = False
    mmcAudio.Command = "Close"
End Sub

' Play the MP3 file.
Private Sub cmdPlay_Click()
    If cmdPlay.Caption = "Play" Then
        mmcAudio.FileName = txtFile.Text
        mmcAudio.Command = "Open"
        mmcAudio.Command = "Play"
        cmdPlay.Caption = "Stop"
    Else
        mmcAudio.Command = "Stop"
        mmcAudio.Command = "Close"
    End If
End Sub

Private Sub mmcAudio_StatusUpdate()
    lblAudio.Caption = mmcAudio.Position & "/" & _
        mmcAudio.Length
End Sub

' Prepare the control to play again.
Private Sub mmcAudio_Done(NotifyCode As Integer)
    mmcAudio.Command = "Close"
    lblAudio.Caption = ""
    cmdPlay.Caption = "Play"
End Sub

Private Sub Form_Unload(Cancel As Integer)
    ' Close the multimedia device.
    mmcAudio.Command = "Close"
End Sub
 
Note that this Zip file doesn't include an MP3 file. You'll have to provide your own.
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated