Search code examples
c#wmpaxwindowsmediaplayer

C# Media Player (WMP) Automatic Next Song


I need to player to automatically go to the next song in the listBox and play it, but it won't play. I have it where it goes to the next song, but when it changes it doesn't start playing. Here is a snippet of the code:

if (axWindowsMediaPlayer1.playState == WMPLib.WMPPlayState.wmppsMediaEnded)
{
    if (listBox1.SelectedIndex != listBox1.Items.Count - 1)
    {
        listBox1.SelectedIndex = listBox1.SelectedIndex + 1;
    }
}

I use this same method for the "Next" and "Previous" buttons and it works perfectly.

I have also tried this:

if (axWindowsMediaPlayer1.playState == WMPLib.WMPPlayState.wmppsMediaEnded)
{
    listBox1.SelectedIndex = listBox1.SelectedIndex + 1;
    axWindowsMediaPlayer1.Ctlcontrols.play();
}

It goes to the next song, but as stated before, just doesn't play.

How can I get it to play?


Solution

  • You can use PlayStateChange Event Handler :

     private void WindowsMediaPlayer1_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
        {
    if (e.newState == 1)
            {
                if (listBox1.SelectedIndex != listBox1.Items.Count - 1)
                {
                    BeginInvoke(new Action(() => {
                        listBox1.SelectedIndex = listBox1.SelectedIndex + 1
                    }));
                }
            }
        }