Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play next file automatically using MediaPlayer Control(AxWindowsMediaPlayer)

When changing AxWindowsMediaPlayer URL in PlayStateChange Event, it doesn't start playing automatically, just changes to "Ready" state.

I have an "AxWindowsMediaPlayer" Control in my C# WinForms program. when I normally change the URL property of WindowsMediaPlayer1, it works fine and plays new mp3 file automatically.

When the song ended WindowsMediaPlayer1 State changes to Stopped and I Want next URL automatically start Playing.

I used PlayStatChange event, so when player state is Stopped, URL Will change, but Not playing automatically!

The player goes to Ready State until I press the play button on the WindowsMediaPlayer1.

Here is the Code:

private void Form1_Load(object sender, EventArgs e)
{
    WindowsMediaPlayer1.URL = "6.mp3"; //Works fine      
}
private void button1_Click(object sender, EventArgs e)
{
    WindowsMediaPlayer1.URL = "4.mp3"; //Works fine. It changes the music.
}
private void WindowsMediaPlayer1_PlayStateChange(object sender, 
    AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
{
    if (e.newState == 1) //1 is for "Stopped" State
        WindowsMediaPlayer1.URL = "5.mp3"; 
    // Here is the problem. 
    // URL Will change but player goes to "Ready" State 
    // But not in "playing" until I press the play button in control.
}

Any help would be appreciated.

like image 405
Mohamad Hedayati Avatar asked Jan 26 '16 12:01

Mohamad Hedayati


1 Answers

As mentioned in media player documentations, you should not set the Url from event handler code. Instead you can play next file this way:

private void axWindowsMediaPlayer1_PlayStateChange(object sender, 
    AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
{
    if (e.newState == 1) 
    {
        this.BeginInvoke(new Action(() => {
            this.axWindowsMediaPlayer1.URL = @"address of nextfile";
        }));
    }   
}

Also as another option you can consider using a playlist.

like image 57
Reza Aghaei Avatar answered Oct 02 '22 05:10

Reza Aghaei