Click here to Skip to main content
Click here to Skip to main content

Play sound in VB.NET

By , 27 Jan 2006
 

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

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionHow to add media file to playlistmemberntquy_it21-Aug-11 5:29 
AnswerRe: How to add media file to playlistmemberAmios20-Jun-12 4:03 
AnswerRe: How to add media file to playlistmemberAmios20-Jun-12 4:04 
GeneralMy vote of 3membercentricomen6-Jul-10 2:05 
GeneralSHort way exist and i will show youmemberekeolere olaide30-Oct-09 13:50 
QuestionStart And End Pointsmemberamiashu5-Oct-07 5:18 
GeneralRe: Play sound in VB.net withmemberZeelia1125-Aug-07 0:59 
GeneralGreat helpmemberNasenbaaer19-Apr-07 7:45 
Questionplaylistmemberchse7204-Mar-07 9:55 
Answerplaylistingmembercentricomen6-Jul-10 2:17 
There are various ways to do this Chase. One way i knew (hopefully i still do) is that you use directories. I used to test it using two controls (typically a listbox for the names and another listbox containing directories).
 
The player would use the "names" listbox to fetch the corresponding directory. So no matter if you are playing the last, first or middle track, it will use directories.
 
Another long and not recommended way is to use loops within a listbox control. Every time you select different track, the loop will search for the folder (same folder) for the item to play. This is kind of combersome thing to do, but once you get used to it, lt'll getting more easier and clearer.
 
Hope this helps Sigh | :sigh:
GeneralMedia Player with SharpdevelopmemberBThunder7-Aug-06 18:34 

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

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