简体   繁体   English

如何自动播放listBox的下一项

[英]How to automatically play next item of a listBox

I am using a listBox to play file from media player on my form, I am using the code below to get files in my listbox,as it retuns the file name, now I am able to play files from listBox and now I want next item in the listbox to be played automatically after a time gap.How to do this 我正在使用listBox从窗体上的媒体播放器播放文件,我正在使用下面的代码在我的列表框中获取文件,因为它重新调整了文件名,现在我可以从listBox播放文件,现在我想要下一个项目在列表框中有一个时间间隔后会自动播放。

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

for listbox I am using the following code 对于列表框,我正在使用以下代码

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

for listBox1 I am using the code 对于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();


    }

Then I am using a button to transfer selected listbox1 files to listBox2 on another form 然后我使用一个按钮将选定的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 code is mentioned above. 上面提到了listbox2代码。

You have to intercept the PlayStateChange of the player. 您必须拦截播放器的PlayStateChange Once you get the int value 8 , which is MediaEnded , you can play the next video in the list after the desired time gap. 获得int8 (即MediaEnded ,您可以在所需的时间间隔后播放列表中的下一个视频。

[UPDATE] - this does not work for .NET 2.0, so get the proper version for Form3.cs from the [EDIT] at the end of the answer [更新]-这不适用于.NET 2.0,因此请在答案末尾的[编辑]中获取Form3.cs的正确版本。

Here's Form3.cs (.NET 4.5): 这是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();
        }
    }
}

And here's Form1.cs (.NET 4.5): 这是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();
        }
    }
}

[EDIT] - this should work for .NET 2.0 [编辑]-这应该适用于.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();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM