Archer
Archer

Reputation: 81

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.

Upvotes: 5

Views: 19145

Answers (4)

Real Programmer
Real Programmer

Reputation: 337

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();

Upvotes: 2

Davio
Davio

Reputation: 4737

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

Upvotes: 3

default locale
default locale

Reputation: 13456

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.

Upvotes: 5

Tiago Peczenyj
Tiago Peczenyj

Reputation: 4623

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.

Upvotes: 0

Related Questions