2

好的,我有问题,我根据列表框中列出的项目制作了这段代码来播放 axmediaplayer。首先,我使用 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]);
            }
        }
    }

然后它使用以下代码在列表框索引更改时播放音乐(当列表框上的项目单击时):

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

它工作正常,然后我希望播放器根据我的列表框上的项目自动移动到下一首歌曲。使用事件 PlayStateChange,所以我制作了这段代码

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

所选索引更改,但播放器不会自动播放下一首歌曲。我必须手动单击播放按钮才能播放列表。谁能帮帮我?

4

2 回答 2

2

好的,我找到了,解决方案是在播放下一首歌曲之前添加计时器。首先我添加了计时器,应该是 timer1。然后我将 playstate 事件更改为如下所示:

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

然后在我添加滴答事件的计时器上,滴答事件是这样的:

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

现在它工作正常^^

于 2012-02-28T16:09:55.057 回答
0

以下功能对我有用

    private void axWindowsMediaPlayer1_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
    {
        if ((WMPLib.WMPPlayState)e.newState == WMPLib.WMPPlayState.wmppsMediaEnded)
        {

            timer1.Interval = 100;
            timer1.Start();
            timer1.Enabled = true;   
            timer1.Tick += timer1_Tick;
        }
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        /// method to play video list items
        myFuntiontoPlayVideo();
        timer1.Enabled = false;
    }     
于 2016-08-09T11:38:39.087 回答