59

I am developing a WinForm application. I want to play a MP3 file when the user clicks a button.

The MP3 file is located in the file system of the computer where the application is executed.
I have Googled for a while and I have found information about the System.Media.SoundPlayer class. But I have read that the SoundPlayer class can only be used to play files in .wav format.

What classes can be used to play files in .mp3 format ?

Any help will be greatly appreciated.

VladL
  • 12,769
  • 10
  • 63
  • 83
user1139666
  • 1,687
  • 4
  • 24
  • 45
  • 1
    Do you need some additional information, or did you already achieve your needs? – Max Feb 22 '13 at 14:17

6 Answers6

44

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.

Max
  • 12,622
  • 16
  • 73
  • 101
  • 7
    Although likely, you cannot be 100% sure Windows Media Player is installed on the target system. – Odys May 20 '15 at 15:16
  • I agree, refer to following question for a check: http://stackoverflow.com/questions/3718152/how-to-check-what-version-of-windows-media-player-is-installed-on-the-machine since I think it is offtopic for this question. – Max May 20 '15 at 15:18
  • It's not as likely as you might think. It's even odds in the UK. Microsoft was sued and forced to release a version of windows without it, and it's what i'm using right now. Not only is it missing media player, the entire media namespace doesn't work and it breaks a lot of media players. – John Lord Aug 13 '19 at 04:38
  • The last line of the mp3 example should be: wplayer.controls.play(); // in lower case. Be sure and add a reference to wmp.dll in the windows\system32 directory – astrosteve Apr 11 '20 at 22:34
40

1) The most simple way would be using WMPLib

WMPLib.WindowsMediaPlayer Player;

private void PlayFile(String url)
{
    Player = new WMPLib.WindowsMediaPlayer();
    Player.PlayStateChange += Player_PlayStateChange;
    Player.URL = url;
    Player.controls.play();
}

private void Player_PlayStateChange(int NewState)
{
    if ((WMPLib.WMPPlayState)NewState == WMPLib.WMPPlayState.wmppsStopped)
    {
        //Actions on stop
    }
}

2) Alternatively you can use the open source library NAudio. It can play mp3 files using different methods and actually offers much more than just playing a file.

This is as simple as

using NAudio;
using NAudio.Wave;

IWavePlayer waveOutDevice = new WaveOut();
AudioFileReader audioFileReader = new AudioFileReader("Hadouken! - Ugly.mp3");

waveOutDevice.Init(audioFileReader);
waveOutDevice.Play();

Don't forget to dispose after the stop

waveOutDevice.Stop();
audioFileReader.Dispose();
waveOutDevice.Dispose();
DAG
  • 867
  • 10
  • 16
VladL
  • 12,769
  • 10
  • 63
  • 83
  • 3
    Voted this up for the suggestion of NAudio. Nice library that did exactly what I needed. I couldn't get ClickOnce to include the WMP assemblies in my manifest. – RameyRoad Oct 19 '18 at 20:11
10
  1. first go to the properties of your project
  2. click on add references
  3. add the library under COM object for window media player then type your code where you want


    Source:

        WMPLib.WindowsMediaPlayer wplayer = new WMPLib.WindowsMediaPlayer();
    
        wplayer.URL = @"C:\Users\Adil M\Documents\Visual Studio 2012\adil.mp3";
        wplayer.controls.play();
    
Community
  • 1
  • 1
Adiii
  • 54,482
  • 7
  • 145
  • 148
2

You can use the mciSendString API to play an MP3 or a WAV file:

[DllImport("winmm.dll")]
public static extern uint mciSendString( 
    string lpstrCommand,
    StringBuilder lpstrReturnString,
    int uReturnLength,
    IntPtr hWndCallback
);

mciSendString(@"close temp_alias", null, 0, IntPtr.Zero);
mciSendString(@"open ""music.mp3"" alias temp_alias", null, 0, IntPtr.Zero);
mciSendString("play temp_alias repeat", null, 0, IntPtr.Zero);
sɐunıɔןɐqɐp
  • 3,332
  • 15
  • 36
  • 40
yditxu
  • 61
  • 5
0

You can do it using old DirectShow functionality.

This answer teaches you how to create QuartzTypeLib.dll:

  1. Run tlbimp tool (in your case path will be different):

  2. Run TlbImp.exe %windir%\system32\quartz.dll /out:QuartzTypeLib.dll

Alternatively, this project contains the library interop.QuartzTypeLib.dll, which is basically the same thing as steps 1. and 2. The following steps teach how to use this library:

  1. Add generated QuartzTypeLib.dll as a COM-reference to your project (click right mouse button on the project name in "Solution Explorer", then select "Add" menu item and then "Reference")

  2. In your Project, expand the "References", find the QuartzTypeLib reference. Right click it and select properties, and change "Embed Interop Types" to false. (Otherwise you won't be able to use the FilgraphManager class in your project (and probably a couple of other ones)).

  3. In Project Settings, in the Build tab, I had to disable the Prefer 32-bit flag, Otherwise I would get this Exception: System.Runtime.InteropServices.COMException: Exception from HRESULT: 0x80040266

  4. Use this class to play your favorite MP3 file:

    using QuartzTypeLib;
    
    public sealed class DirectShowPlayer
    {
        private FilgraphManager FilterGraph;
    
        public void Play(string path)
        {
            FilgraphManager = new FilgraphManager();
            FilterGraph.RenderFile(path);
            FilterGraph.Run();
        }
    
        public void Stop()
        {
            FilterGraph?.Stop();
        }
    }
    

PS: TlbImp.exe can be found here: "C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin", or in "C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.7.2 Tools"

sɐunıɔןɐqɐp
  • 3,332
  • 15
  • 36
  • 40
-3

Refactoring:

new WindowsMediaPlayer() { URL = "MyMusic.mp3" }.controls.play();