I have this code which checks if a song has ended, and if it has selects the next one. I have the song names in a ListBox, so when the next song gets selected the first function triggers. Can you explain me why it doesn't play the song?
private void Files_SelectedIndexChanged(object sender, EventArgs e)
{
player.URL = percorsi[Files.SelectedIndex];
}
private void player_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent er)
{
if (er.newState == 8)
{
Files.SetSelected((Files.SelectedIndex + 1) % nomi.Length , true);
}
}
Microsoft's help page for the URL property has the following comment.
Do not call this method from event handler code. Calling URL from an event handler may yield unexpected results.
http://msdn.microsoft.com/en-us/library/windows/desktop/dd562470(v=vs.85).aspx
You can also see this previous post.
Playing two video with axWindowsMediaPlayer
The solution I came up with, though not the best, was to create a Timer on the Form and implemented the _Tick handler. Then in the Form I also created a Boolean (initialized to false) to indicate that a new file should be played.
private void axWindowsMediaPlayer1_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
{
if (e.newState == 8)
{
Files.SelectedIndex = File.SelectedIndex + 1;
}
}
private void Files_SelectedIndexChanged(object sender, EventArgs e)
{
playNewFile = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
if (playNewFile)
{
axWindowsMediaPlayer1.URL = percorsi[Files.SelectedIndex];
playNewFile = false;
}
}
I set the Timer Interval for 100 ms and started it in the Form_Load event.
private void Form1_Load(object sender, EventArgs e)
{
timer1.Interval = 100;
timer1.Start();
}