How to play next item on playlist with axmediaplayer?

Cero picture Cero · Feb 3, 2012 · Viewed 9.3k times · Source

ok i have question, i made this code to play axmediaplayer base on item listed on listbox. first i make this code to make a list using opendialog :

 private string[] files, path;
 private void button1_Click(object sender, EventArgs e)
    {
        if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            files = openFileDialog1.SafeFileNames;
            path = openFileDialog1.FileNames;
            for (int i = 0; i < files.Length; i++) {
                listBox1.Items.Add(files[i]);
            }
        }
    }

and then it play the music when the listbox index changed (when the item on the list box cliked) using this code :

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    axWindowsMediaPlayer1.URL = path[listBox1.SelectedIndex];
}

it works fine, and then i want player to automove to the next song base on item on my listbox. with using events PlayStateChange, so i make this code

private void axWindowsMediaPlayer1_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
{
    if (axWindowsMediaPlayer1.playState == WMPLib.WMPPlayState.wmppsMediaEnded) 
    {
         if(listBox1.SelectedIndex < files.Length - 1)
         {
            listBox1.SelectedIndex = listBox1.SelectedIndex + 1;
         }
    }
}

the selected index change, but the player doesn't auto play the next song. i must click the play button manually in order to play the list. can anyone help me up?

Answer

Cero picture Cero · Feb 28, 2012

ok i found it, the solution is to add timer before playing the next song. first im adding timer, that shoud be timer1. and then i change playstate event to something like this :

private void axWindowsMediaPlayer1_PlayStateChange(object sender, axWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
    {
        if (axWindowsMediaPlayer1.playState == WMPLib.WMPPlayState.wmppsMediaEnded)
        {
            timer1.Interval = 100;
            timer1.Enabled = true;               
        }            
     }

then on the timer i adding tick event, the tick event is something like this :

 private void timer1_Tick(object sender, EventArgs e)
    {
        if (listBox1.SelectedIndex < files.Length - 1)
        {
            listBox1.SelectedIndex++;
            timer1.Enabled = false;
        }
        else
        {
            listBox1.SelectedIndex = 0;
            timer1.Enabled = false;
        }            
    }       

now its work fine ^^