Tip: C# – Detect the End of Media – axWindowsMediaPlayer

I saw too many articles regarding how to detect the end of a song using axWindowsMediaPlayer.

A lot of people say you have to watch the axWindowsMediaPlayer.status until it actually becomes “Stopped”.

That is wrong…

Why? What if the user presses the Stop button for your application? It returns “Stopped” too, so that won’t work.

axWindowsMediaPlayer.EndOfStream is still not good. But some people think it is.

The trick is to use the axWindowsMediaPlayer.PlayStateChanged().

Basically, this will let you monitor every single state of your axWindowsMediaPlayer control.
Here is a list of all the states.

0 = Undefined
1 = Stopped (by User)
2 = Paused
3 = Playing
4 = Scan Forward
5 = Scan Backwards
6 = Buffering
7 = Waiting
8 = Media Ended
9 = Transitioning
10 = Ready
11 = Reconnecting
12 = Last

So, from that list you should know that you must use 8.

In the event we will do the following:

private void axWindowsMediaPlayer1_PlayStateChange(
    object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e) {
    if (e.newState == 8) {
        //Your Code Here
    }
}

That’s basically it.

Hope this helps a lot of people, but most importantly YOU The Reader!

ClayC

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read