5

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.

default locale
  • 13,035
  • 13
  • 56
  • 62
Archer
  • 81
  • 1
  • 3
  • 7

4 Answers4

5

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.

default locale
  • 13,035
  • 13
  • 56
  • 62
3

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

Davio
  • 4,609
  • 2
  • 31
  • 58
2

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();
Real Programmer
  • 331
  • 2
  • 13
0

you can add something like SoundManager 2 and in the onClick event you can call the method play()

http://www.schillmania.com/projects/soundmanager2/doc/getstarted/

I don't know if there is something already done for C# to do this.

Tiago Peczenyj
  • 4,387
  • 2
  • 22
  • 35