【问题标题】:How to automatically play next item of a listBox如何自动播放列表框的下一项
【发布时间】:2013-01-15 10:51:28
【问题描述】:

我正在使用列表框在我的表单上播放来自媒体播放器的文件,我正在使用下面的代码在我的列表框中获取文件,因为它会重新调整文件名,现在我可以从列表框播放文件,现在我希望在时间间隔后自动播放列表框中的下一项。如何做到这一点

this.listBox1.DisplayMember = "Name";/*to display name on listbox*/
this.listBox1.ValueMember = "FullName";/*to fetch item value on listbox*/
listBox1.DataSource = GetFolder("..\\video\\"); /*gets folder path*/ 

private static List<FileInfo> GetFolder(string folder)
{
    List<FileInfo> fileList = new List<FileInfo>();

    foreach (FileInfo file in new DirectoryInfo(folder)
                                 .GetFiles("*.mpg",SearchOption.AllDirectories))
    {
        fileList.Add(file);
    }

    return fileList;
}

对于列表框,我使用以下代码

private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
{     
    Player.URL = Convert.ToString(listBox2.SelectedItem);
}

对于listBox1,我使用的是代码

    private void listBox1_SelectedIndexChanged(object sender, EventArgs e )
    {

        StreamWriter sw = new StreamWriter("..\\Debug\\List.txt", true);
        //Player.URL = Convert.ToString(listBox1.SelectedItem);
        string selectedItem = listBox1.Items[listBox1.SelectedIndex].ToString();


        //listView1.Items.Add(listBox1.SelectedItem.ToString());

        foreach (object o in listBox1.SelectedItems)
            sw.WriteLine(DateTime.Now + " - " + o);

        sw.Close();


    }

然后我使用按钮将选定的 listbox1 文件传输到另一个表单上的 listBox2

   private void button1_Click_1(object sender, EventArgs e)
    {

        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        foreach (object item in listBox1.Items)
        {
            sb.Append(item.ToString());
            sb.Append(" ");

        }


       string selectedItem = listBox1.Items[listBox1.SelectedIndex].ToString();


            //listBox2.Items.Add(listBox1.SelectedItem.ToString());
            Form3 frm = new Form3();

            foreach (int i in listBox1.SelectedIndices)
            {

                    frm.listBox2.Items.Add(listBox1.Items[i].ToString());


                frm.Show();
                this.Hide();

            }

        }

listbox2的代码在上面提到了。

【问题讨论】:

  • 你用的是什么播放器?您能否简单地挂钩一些事件,让您知道它何时完成以再次更改 .URI
  • 我正在使用 axWindowsMediaPlayer

标签: c# .net winforms


【解决方案1】:

你必须拦截播放器的PlayStateChange。一旦获得int8,即MediaEnded,您就可以在所需的时间间隔后播放列表中的下一个视频。

[UPDATE] - 这不适用于 .NET 2.0,因此请从答案末尾的 [EDIT] 获取 Form3.cs 的正确版本

这里是Form3.cs (.NET 4.5):

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WMPLib;

namespace WindowsFormsApplication10
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();

            this.listBox2.DisplayMember = "Name";/*to display name on listbox*/
            this.listBox2.ValueMember = "FullName";/*to fetch item value on listbox*/
            Player.PlayStateChange += Player_PlayStateChange;
        }

        async void Player_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
        {
            if ((WMPLib.WMPPlayState)e.newState == WMPLib.WMPPlayState.wmppsMediaEnded)
            {
                if (listBox2.SelectedIndex + 1 < listBox2.Items.Count)
                {
                    await System.Threading.Tasks.Task.Delay(3000);
                    listBox2.SelectedItem = listBox2.Items[listBox2.SelectedIndex + 1];
                }
            }
        }

        private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            Player.URL = Convert.ToString(listBox2.SelectedItem.GetType().GetProperty("FullName").GetValue(listBox2.SelectedItem)).ToString();
            Player.Ctlcontrols.play();
        }
    }
}

这里是Form1.cs (.NET 4.5):

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
//using System.Linq;
using System.Text;
//using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication10
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.listBox1.DisplayMember = "Name";/*to display name on listbox*/
            this.listBox1.ValueMember = "FullName";/*to fetch item value on listbox*/
            listBox1.DataSource = GetFolder("..\\video\\"); /*gets folder path*/ 
        }

        private static List<FileInfo> GetFolder(string folder)
        {
            List<FileInfo> fileList = new List<FileInfo>();
            foreach (FileInfo file in new DirectoryInfo(folder)
                                         .GetFiles("*.mpg", SearchOption.AllDirectories))
            {
                fileList.Add(file);
            }
            return fileList;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form3 frm = new Form3();
            frm.FormClosed += frm_FormClosed;

            foreach (int i in listBox1.SelectedIndices)
            {
                frm.listBox2.Items.Add(new 
                { 
                    Name = ((FileInfo)listBox1.Items[i]).Name, 
                    FullName = ((FileInfo)listBox1.Items[i]).FullName 
                });
                frm.Show();
                this.Hide();
            }
        }

        void frm_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.Show();
        }
    }
}

[编辑] - 这应该适用于 .NET 2.0

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WMPLib;

namespace WindowsFormsApplication10
{
    public partial class Form3 : Form
    {
        System.Timers.Timer _timer = new System.Timers.Timer();
        object _locker = new object();

        public Form3()
        {
            InitializeComponent();

            this.listBox2.DisplayMember = "Name";/*to display name on listbox*/
            this.listBox2.ValueMember = "FullName";/*to fetch item value on listbox*/
            Player.PlayStateChange += Player_PlayStateChange;

            _timer.Elapsed += _timer_Elapsed;
            _timer.Interval = 3000;

        }

        void _timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            _timer.Stop();
            lock (_locker)
            {
                this.Invoke((MethodInvoker)delegate
                {
                    if (listBox2.SelectedIndex + 1 < listBox2.Items.Count)
                    {
                        listBox2.SelectedItem = listBox2.Items[listBox2.SelectedIndex + 1];
                    }
                });
            }
        }

        void Player_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
        {
            if ((WMPLib.WMPPlayState)e.newState == WMPLib.WMPPlayState.wmppsMediaEnded)
            {
                _timer.Start();
            }
            else if ((WMPLib.WMPPlayState)e.newState == WMPLib.WMPPlayState.wmppsReady)
            {
                Player.Ctlcontrols.play();
            }
        }

        private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            Player.URL = Convert.ToString(listBox2.SelectedItem.GetType().GetProperty("FullName").GetValue(listBox2.SelectedItem, null)).ToString();
        }
    }
}

【讨论】:

  • Alex 我试过你的代码,但它显示了一些错误 错误 1 ​​Invalid token 'void' in class, struct, or interface member declaration D:\.net\WindowsApplication1\WindowsApplication1\Form3.cs 52 15当我将异步更改为私有时,WindowsApplication1 显示错误 1 ​​方法 'GetValue' 没有重载需要 '1' 参数 D:\.net\WindowsApplication1\WindowsApplication1\Form3.cs 45 43 WindowsApplication1 并且在线等待也有错误..
  • 什么错误?您是否通过表单的设计器添加了播放器?
  • 好的,我将发布整个文件内容。可能有一些无与伦比的括号。
  • 好的,把Form3.cs的代码贴出来了,Form1.cs的代码也加了。
  • Alex 代码对我不起作用,兄弟..System.Linq 和 System.Threading 显示命名空间错误..我想告诉我我正在为此使用 Visual Studio 2005。所以就是这样有问题吗?
猜你喜欢
  • 2012-05-16
  • 2014-11-20
  • 2021-01-17
  • 1970-01-01
  • 1970-01-01
  • 2019-12-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多