Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Playing a mp3 file at button click event in windows form

How to play a .mp3 file during the button click event in windows form?

I'm new to C# and any pointers to this will be really helpful.

like image 810
Archer Avatar asked May 15 '12 05:05

Archer


3 Answers

You can use WindowsMediaPlayer COM control to play mp3.

Here is a guide from msdn.

WMPLib.WindowsMediaPlayer player = new WMPLib.WindowsMediaPlayer();
player.URL = @"track.mp3";
player.controls.play();

Also you can use SoundPlayer to play wav files. There are also several third party .net mp3 libraries.

like image 141
default locale Avatar answered Nov 13 '22 14:11

default locale


I wonder if you can 'steal' WPF's MediaPlayer which is like the mentioned SoundPlayer, but also capable of playing MP3.

var player = new System.Windows.Media.MediaPlayer();
player.Open(new System.Uri("myfile.mp3"));
player.Play();

Haven't tried it, but it could work and this way you don't need to use an external library. More info on MSDN: http://msdn.microsoft.com/en-us/library/system.windows.media.mediaplayer

like image 38
Davio Avatar answered Nov 13 '22 13:11

Davio


you must use .wav sound files , tht's the easiest and best way to play a sound clip

System.Media.SoundPlayer s = new System.Media.SoundPlayer();
              s.SoundLocation = "C:\\mps.wav" ; 
              s.Load() ; 
              s.Play();
like image 28
Real Programmer Avatar answered Nov 13 '22 14:11

Real Programmer