Play next file automatically using MediaPlayer Control(AxWindowsMediaPlayer)
Asked Answered
R

2

5

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.

Ranaerancagua answered 26/1, 2016 at 12:45 Comment(0)
V
8

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.

Veliz answered 26/1, 2016 at 14:40 Comment(0)
R
0

I found this note on msdn about player.URL:

"Do not call this method from event handler code. Calling URL from an event handler may yield unexpected results."

so I tried another way to solve it and its worked.

added a timer and a bool varible to check if WindowsMediaPlayer1 is "Stopped"

Here is the solution:

 public partial class Form1 : Form
{
    bool nextURL = false;
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        WindowsMediaPlayer1.URL = "5.mp3";                   
    }

    private void WindowsMediaPlayer1_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
    {
        if (e.newState == 1) // 1 is consider for  "Stopped" State
        {
            nextURL = true;   // if the song ended "nextURL" flag sets to true
        }   
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        if (nextURL)
        {
            WindowsMediaPlayer1.URL = "6.mp3";
            nextURL = false;
        }
    }
Ranaerancagua answered 26/1, 2016 at 14:19 Comment(1)
You don't need to use a timer, you can simply use this.BeginInvoke to set the url. it's the same as changing url after that event.Veliz

© 2022 - 2024 — McMap. All rights reserved.