如何使用 axmediaplayer 播放播放列表中的下一个项目?

发布于 2025-01-02 00:08:12 字数 1241 浏览 1 评论 0原文

好的,我有问题,我编写了此代码来根据列表框中列出的项目播放 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;
         }
    }
}

更改为选定的索引,但播放器不会自动播放下一首歌曲。我必须手动单击播放按钮才能播放列表。有人可以帮我吗?

ok i have question, i made this code to play axmediaplayer base on item listed on listbox.
first i make this code to make a list using 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]);
            }
        }
    }

and then it play the music when the listbox index changed (when the item on the list box cliked) using this code :

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

it works fine, and then i want player to automove to the next song base on item on my listbox. with using events PlayStateChange, so i make this code

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

the selected index change, but the player doesn't auto play the next song. i must click the play button manually in order to play the list. can anyone help me up?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小霸王臭丫头 2025-01-09 00:08:12

好的,我找到了,解决方案是在播放下一首歌曲之前添加计时器。
首先我添加计时器,应该是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;
        }            
    }       

现在它工作正常 ^^

ok i found it, the solution is to add timer before playing the next song.
first im adding timer, that shoud be timer1. and then i change playstate event to something like this :

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

then on the timer i adding tick event, the tick event is something like this :

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

now its work fine ^^

盗琴音 2025-01-09 00:08:12

以下功能对我有用

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

Below functionality worked for me:

    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;
    }     
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文