Author Topic: Using VB.net Play an MP3 audio file  (Read 3164 times)

nandagopal

  • Old Members
  • Hero Member
  • *
  • Posts: 5720
    • View Profile
Using VB.net Play an MP3 audio file
« on: November 21, 2008, 08:08:14 pm »
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.
 

Code: [Select]
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