Click here to Skip to main content
Licence CPOL
First Posted 27 Jan 2006
Views 135,378
Downloads 2,397
Bookmarked 38 times

Play sound in VB.NET

By Hofstetter Daniel | 27 Jan 2006
This sample presents a simple solution to play sound (MP3, Wav etc..) in a VB.NET (2003) solution.
3 votes, 27.3%
1
1 vote, 9.1%
2
2 votes, 18.2%
3
1 vote, 9.1%
4
4 votes, 36.4%
5
3.25/5 - 11 votes
μ 3.25, σa 3.01 [?]

Introduction

The Windows Media Player control lets 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 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:

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)

About the Author

Hofstetter Daniel

Web Developer

Switzerland Switzerland

Member
VBA and .net software writer.
For details see my website www.daniel-hofstetter.ch

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionHow to add media file to playlist Pinmemberntquy_it6:29 21 Aug '11  
GeneralMy vote of 3 Pinmembercentricomen3:05 6 Jul '10  
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  
Answerplaylisting Pinmembercentricomen3:17 6 Jul '10  
GeneralMedia Player with Sharpdevelop PinmemberBThunder19:34 7 Aug '06  
Great Article, also, you know how to program a MP3 player with Sharpdevelop in vb.net?

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

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120210.1 | Last Updated 27 Jan 2006
Article Copyright 2006 by Hofstetter Daniel
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid