Mediaplayer WinForm无法播放 [英] Mediaplayer WinForm won't play

查看:113
本文介绍了Mediaplayer WinForm无法播放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段代码可以检查一首歌是否结束,以及是否选择了下一首.我有一个ListBox中的歌曲名称,所以当下一首歌曲被选中的第一个函数触发器.你能解释一下为什么它不播放这首歌吗?

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

推荐答案

Microsoft的URL属性帮助页面有以下注释.

Microsoft's help page for the URL property has the following comment.

不要从事件处理程序代码中调用此方法.从事件处理程序调用URL可能会产生意外的结果.

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

http://msdn.microsoft.com/zh-CN/library/windows/desktop/dd562470(v=vs.85).aspx

您也可以看到此以前的帖子.

You can also see this previous post.

使用axWindowsMediaPlayer播放两个视频

我想出的解决方案虽然不是最好的,但它是在Form上创建一个Timer并实现_Tick处理程序.然后,在表单中,我还创建了一个布尔值(初始化为false)以指示应播放一个新文件.

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

我将计时器间隔设置为100毫秒,并在Form_Load事件中启动它.

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

这篇关于Mediaplayer WinForm无法播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆