Playing a Mp3 File in a Winform Application

Playing a MP3 file in a WinForm application

The link below, gives a very good tutorial, about playing mp3 files from a windows form with c#:

http://www.daniweb.com/software-development/csharp/threads/292695/playing-mp3-in-c

This link will lead you to a topic, which contains a lot information about how to play an mp3 song, using Windows forms. It also contains a lot of other projects, trying to achieve the same thing:

http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/3dbfb9a3-4e14-41d1-afbb-1790420706fe

For example use this code for .mp3:

WMPLib.WindowsMediaPlayer wplayer = new WMPLib.WindowsMediaPlayer();

wplayer.URL = "My MP3 file.mp3";
wplayer.Controls.Play();

Then only put the wplayer.Controls.Play(); in the Button_Click event.

For example use this code for .wav:

System.Media.SoundPlayer player = new System.Media.SoundPlayer();

player.SoundLocation = "Sound.wav";
player.Play();

Put the player.Play(); in the Button_Click event, and it will work.

C# play embedded mp3 file

To play an mp3 file, You will need to add the windows media player Library.
1. Add a reference to the WMP library - in your solution explorer, under your project, go to References, right click and add a reference to WMP. It will be under the COM libraries, I think.
2. Add "using WMPLib;" to your code at the top,
3. Add this code to play the file:

        WindowsMediaPlayer myplayer = new WindowsMediaPlayer();
myplayer.URL = "mysound.mp3";
myplayer.controls.play();

mind you the URL is the path to the file you want to play. If it has backslashes in it, you will have to "escape" them. Like this

 myplayer.URL = "C:\\Users\\Public\\Music\\Sample Music\\Kalimba.mp3";

Playing MP3 file using C#

I have written an open source library called NAudio that can do this:

private IWavePlayer waveOut;
private Mp3FileReader mp3FileReader;

private void PlayMp3()
{
this.waveOut = new WaveOut(); // or new WaveOutEvent() if you are not using WinForms/WPF
this.mp3FileReader = new Mp3FileReader("myfile.mp3");
this.waveOut.Init(mp3FileReader);
this.waveOut.Play();
this.waveOut.PlaybackStopped += OnPlaybackStopped;
}

private void OnPlaybackStopped(object sender, EventArgs e)
{
this.waveOut.Dispose();
this.mp3FileReader.Dispose();
}

how to play mp3 file in c# windows form visual studio 2013 windows8

This code has to run perfectly as I´ve tested it:

WMPLib.WindowsMediaPlayer wplayer = new WMPLib.WindowsMediaPlayer();

wplayer.URL = "My MP3 file.mp3";
wplayer.controls.play();


Related Topics



Leave a reply



Submit