Click here to Skip to main content
6,595,854 members and growing! (18,401 online)
Email Password   helpLost your password?
Languages » VB.NET » General     Intermediate

Play sound in VB.net with

By Hofstetter Daniel

This sample present a simple solution to play sound (mp3, Wav etc..) in a VB.Net (2003) solution.
VB.NET 1.1, Win2K, WinXP, WinForms, VS.NET2003, Dev
Posted:27 Jan 2006
Views:74,724
Bookmarked:30 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
9 votes for this article.
Popularity: 3.00 Rating: 3.14 out of 5
3 votes, 33.3%
1
1 vote, 11.1%
2
1 vote, 11.1%
3
1 vote, 11.1%
4
3 votes, 33.3%
5

Introduction

A Windows Media Player Control let you play mp3 or wav files with a unique line of vb.net code:

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 Iitems�

Sample screenshot 
 


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

Sample screenshot

 

 

Confirm with OK Button.

 

Step 3

Add the control to the Main form.
Select the Windows Media Player in the toolbox and ad 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.

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Hofstetter Daniel


Member
VBA and .net software writer.
For details see my website www.daniel-hofstetter.ch
Occupation: Web Developer
Location: Switzerland Switzerland

Other popular VB.NET articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 6 of 6 (Total in Forum: 6) (Refresh)FirstPrevNext
GeneralSHort way exist and i will show you Pinmemberekeolere olaide14:50 30 Oct '09  
QuestionStart And End Points Pinmemberamiashu6:18 5 Oct '07  
GeneralRe: Play sound in VB.net with PinmemberZeelia111:59 25 Aug '07  
GeneralGreat help PinmemberNasenbaaer8:45 19 Apr '07  
Questionplaylist Pinmemberchse72010:55 4 Mar '07  
GeneralMedia Player with Sharpdevelop PinmemberBThunder19:34 7 Aug '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 27 Jan 2006
Editor:
Copyright 2006 by Hofstetter Daniel
Everything else Copyright © CodeProject, 1999-2009
Web19 | Advertise on the Code Project