Using VB.net Play an MP3 audio file

Started by nandagopal, Nov 21, 2008, 08:08 pm

previous topic - next topic
Go Down

nandagopal

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



Go Up
 

Quick Reply

With Quick-Reply you can write a post when viewing a topic without loading a new page. You can still use bulletin board code and smileys as you would in a normal post.

Warning: this topic has not been posted in for at least 120 days.
Unless you're sure you want to reply, please consider starting a new topic.

Note: this post will not display until it's been approved by a moderator.
Name:
Email:
Verification:
Please leave this box empty:

Type the letters shown in the picture
Listen to the letters / Request another image

Type the letters shown in the picture:

shortcuts: alt+s submit/post or alt+p preview