Mediaplayer WinForm won't play

丶灬走出姿态 提交于 2019-12-25 02:19:54

问题


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);
    }
}

回答1:


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();
    }


来源:https://stackoverflow.com/questions/22564891/mediaplayer-winform-wont-play

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!