Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
VB

Hi,

I am trying to play an MP3 file on a button click event. I hav ethe MP3 file named "introduction.mp3" which I have added as an existing file to my project, and I have set it's properties

Build Action - Content
Copy to Output Directory - Copy Always.

I then have the following code in my button click event,

VB
mPlayer.Open(New Uri("Introduction.mp3", UriKind.Relative))
MessageBox.Show("The audio should be playing", "Playing", MessageBoxButton.OK)
mPlayer.Play()


mPlayer is a MediaPlayer object that I have declared at the top of the class,

VB
Dim mPlayer As New MediaPlayer


I have checked that the file has copied to the output directory and plays when run in widnows media player.

When I click the button the files does not play, can anyone tell me what I am missing, the message box does show so I know the event is firing but there is no sound.

Does anyone have any ideas I have been searching online for hours and all I seem to find is examples of this code which does not work for me.

Any help would be great

Thanks
Posted

I'm pretty sure you are not forming your URI correctly. Use the message box to show the URI location and I would bet that its not your application directory.

You could do something like this:

VB
Dim fileUri as Uri = New Uri(Application.StartupPath + @"\Introduction.mp3", UriKind.Absolute)
mPlayer.Open()
MessageBox.Show("The audio should be playing " & fileUri.ToString(), "Playing", MessageBoxButton.OK)
mPlayer.Play()


(You may need to tweak, I'm not a VB programmer)

You should also set your Build action to "None" instead of Content.
 
Share this answer
 
It really does help if you remember to import everything you need, I had missed

VB
imports System.Media


MediaPlayer does not seem to throw errors for things like this.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 11-Dec-13 11:02am    
Fixing your own trivial mistake could be considered as the "solution", even though it is a solution for you, it's doesn't say anything to anyone else. Anything can be missing and later remembered, this is not a reason to post about it. You could simply modify your question to mark that you no longer need it, or remove it.
—SA

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900