Click here to Skip to main content
15,867,290 members
Articles / Desktop Programming / Windows Forms

Play sound in VB.NET

Rate me:
Please Sign up or sign in to vote.
3.47/5 (14 votes)
27 Jan 2006CPOL 254.8K   5.9K   44   11
This sample presents a simple solution to play sound (MP3, Wav etc..) in a VB.NET (2003) solution.

Introduction

The Windows Media Player control lets you play MP3 or Wav files with a unique line of VB.NET code:

VB
Me.AxWindowsMediaPlayer1.URL = dlgFileDialog.FileName

Step by step explanation

Step 1

Create a new VBA Windows application in Visual Studio.

Step 2

Add the Media Player control to the toolbox. Right-click the toolbox and choose Add/Remove Items...

Sample screenshot

In the Customize Toolbox window, chose the COM Components panel, and navigate to Windows Media Player and select it.

Sample screenshot

Confirm with the OK button.

Step 3

Add the control to the main form. Select Windows Media Player in the toolbox and add it to the main form.

Sample screenshot

Step 4

Change the Visible property of the control to False.

Sample screenshot

Step 5

Add a Button control to the main form:

Sample screenshot

Step 6

Double-click the Button and add the following code to the Click event procedure:

VB
Private Sub Button1_Click(ByVal sender As System.Object, _
                          ByVal e As System.EventArgs) _
                          Handles Button1.Click 
    Const DATA_FILE_EXTENSION As String = ".mp3"
    Dim dlgFileDialog As New OpenFileDialog 
    With dlgFileDialog
        .Filter = DATA_FILE_EXTENSION & _
        " files (*" & DATA_FILE_EXTENSION & "|*" & DATA_FILE_EXTENSION 
        .FilterIndex = 1
        .RestoreDirectory = True 
        If .ShowDialog() = DialogResult.OK Then 
            'Play the sound file
         Me.AxWindowsMediaPlayer1.URL = dlgFileDialog.FileName 
        End If 
    End With 
End Sub

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Switzerland Switzerland
VBA and .net software writer.
For details see my website www.daniel-hofstetter.ch

Comments and Discussions

 
QuestionHow to add media file to playlist Pin
ntquy_it21-Aug-11 5:29
ntquy_it21-Aug-11 5:29 
AnswerRe: How to add media file to playlist Pin
Amios20-Jun-12 4:03
Amios20-Jun-12 4:03 
AnswerRe: How to add media file to playlist Pin
Amios20-Jun-12 4:04
Amios20-Jun-12 4:04 
i know a way for that..
reply if you are want to know that..
thanks.
GeneralMy vote of 3 Pin
centricomen6-Jul-10 2:05
centricomen6-Jul-10 2:05 
GeneralSHort way exist and i will show you Pin
ekeolere olaide30-Oct-09 13:50
ekeolere olaide30-Oct-09 13:50 
QuestionStart And End Points Pin
amiashu5-Oct-07 5:18
amiashu5-Oct-07 5:18 
GeneralRe: Play sound in VB.net with Pin
Zeelia1125-Aug-07 0:59
Zeelia1125-Aug-07 0:59 
GeneralGreat help Pin
Nasenbaaer19-Apr-07 7:45
Nasenbaaer19-Apr-07 7:45 
Questionplaylist Pin
chse7204-Mar-07 9:55
chse7204-Mar-07 9:55 
Answerplaylisting Pin
centricomen6-Jul-10 2:17
centricomen6-Jul-10 2:17 
GeneralMedia Player with Sharpdevelop Pin
BThunder7-Aug-06 18:34
BThunder7-Aug-06 18:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.