Playing a MP3 file in a WinForm application

2019-01-05 01:32发布

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.

5条回答
成全新的幸福
2楼-- · 2019-01-05 02:14
  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();
    
查看更多
老娘就宠你
3楼-- · 2019-01-05 02:15

Refactoring:

new WindowsMediaPlayer() { URL = "MyMusic.mp3" }.controls.play();
查看更多
Emotional °昔
4楼-- · 2019-01-05 02:18

you can use the mciSendString API to play an mp3 or a wav file like this

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

mciSendString(@"close temp_alias", null, 0, 0);   
mciSendString(@"open ""music.mp3"" alias temp_alias", null, 0, 0);   
mciSendString("play temp_alias repeat", null, 0, 0); 
查看更多
Juvenile、少年°
5楼-- · 2019-01-05 02:20

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.

查看更多
【Aperson】
6楼-- · 2019-01-05 02:21

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();
查看更多
登录 后发表回答