My c# windows form is enable to play an mp3 file.I did this using this code
WMPLib.WindowsMediaPlayer wplayer;
wplayer = new WMPLib.WindowsMediaPlaye
You can do it by using the PlayStateChanged event. you can add it to your MediaPlayer like this.
WMPLib.WindowsMediaPlayer wplayer;
wplayer = new WMPLib.WindowsMediaPlayer();
wplayer.PlayStateChange += new WMPLib._WMPOCXEvents_PlayStateChangeEventHandler(wplayer_PlayStateChange);
wplayer.URL = "c:/Standup.mp3";
wplayer.controls.play();
you can then check for the MediaEnded PlayState in the EventHandler and reset the currentPosition to the start of the song :
void wplayer_PlayStateChange(int NewState)
{
if (NewState == (int)WMPLib.WMPPlayState.wmppsMediaEnded)
{
wplayer.controls.currentPosition = 0;
}
}
Edit: I expected to be able to make a song repeatable to the point I was sick of it, and the above code did work when I had breakpoints set. Once I removed them I found there were other PlayStates that were stopping the file from being played. I was able to bypass it by using a one shot timer.. Now I am tired of the song that I was using. There may/probably be a better way of doing this, but this will work.
Modified Code
public partial class Form1 : Form
{
WMPLib.WindowsMediaPlayer wplayer;
Timer tmr = new Timer();
public Form1()
{
InitializeComponent();
tmr.Interval = 10;
tmr.Stop();
tmr.Tick += new EventHandler(tmr_Tick);
wplayer = new WMPLib.WindowsMediaPlayer();
wplayer.URL = "c:/Standup.mp3";
wplayer.PlayStateChange += new WMPLib._WMPOCXEvents_PlayStateChangeEventHandler(wplayer_PlayStateChange);
wplayer.controls.play();
}
void tmr_Tick(object sender, EventArgs e)
{
tmr.Stop();
wplayer.controls.play();
}
void wplayer_PlayStateChange(int NewState)
{
if (NewState == (int)WMPLib.WMPPlayState.wmppsMediaEnded )
{
tmr.Start();
}
}
}
If you don't necessarily need to know when the file has completed for any other purposes than looping, you may consider the setMode method to turn track looping on.
http://msdn.microsoft.com/en-us/library/windows/desktop/dd564867(v=vs.85).aspx
You can check it constantly with a Thread however, there is little documentation...
//player .playState
//Possible Values
//
//This property is a read-only Number (long). The C-style enumeration constant can be derived by prefixing
//the state value with "wmpps". For example, the constant for the Playing state is wmppsPlaying.
//Value State Description
//0 Undefined Windows Media Player is in an undefined state.
//1 Stopped Playback of the current media item is stopped.
//2 Paused Playback of the current media item is paused. When a media item is paused, resuming playback begins from the same location.
//3 Playing The current media item is playing.
//4 ScanForward The current media item is fast forwarding.
//5 ScanReverse The current media item is fast rewinding.
//6 Buffering The current media item is getting additional data from the server.
//7 Waiting Connection is established, but the server is not sending data. Waiting for session to begin.
//8 MediaEnded Media item has completed playback.
//9 Transitioning Preparing new media item.
//10 Ready Ready to begin playing.
//11 Reconnecting Reconnecting to stream.
You can use PlayStateChange(int NewState) function bulitinto media player to detect for stopped state.