C# 如何自动播放列表框的下一项
我正在使用列表框在我的表单上播放媒体播放器中的文件,我正在使用下面的代码获取列表框中的文件,因为它会重新运行文件名,现在我可以播放列表框中的文件,现在我希望列表框中的下一项在一段时间间隔后自动播放。如何做到这一点C# 如何自动播放列表框的下一项,c#,.net,winforms,C#,.net,Winforms,我正在使用列表框在我的表单上播放媒体播放器中的文件,我正在使用下面的代码获取列表框中的文件,因为它会重新运行文件名,现在我可以播放列表框中的文件,现在我希望列表框中的下一项在一段时间间隔后自动播放。如何做到这一点 this.listBox1.DisplayMember = "Name";/*to display name on listbox*/ this.listBox1.ValueMember = "FullName";/*to fetch item value on listbox*/ l
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;
}
对于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代码。您必须拦截播放器的输入。一旦获得int
值8
,即MediaEnded
,您就可以在所需的时间间隔后播放列表中的下一个视频
[UPDATE]-这不适用于.NET 2.0,因此从答案末尾的[EDIT]获取Form3.cs
的正确版本
以下是Form3.cs
(.NET4.5):
[编辑]-这应该适用于.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();
}
}
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用系统文本;
使用System.Windows.Forms;
使用WMPLib;
命名空间Windows窗体应用程序10
{
公共部分类表单3:表单
{
System.Timers.Timer _Timer=新的System.Timers.Timer();
对象_locker=新对象();
公共表格3()
{
初始化组件();
this.listBox2.DisplayMember=“Name”/*在列表框上显示名称*/
this.listBox2.ValueMember=“FullName”/*获取列表框上的项目值*/
Player.PlayStateChange+=Player\u PlayStateChange;
_timer.appeated+=\u timer\u appeated;
_定时器间隔=3000;
}
无效_timer_已过(对象发送器,System.Timers.ElapsedEventArgs e)
{
_timer.Stop();
锁(储物柜)
{
此.Invoke((MethodInvoker)委托
{
如果(listBox2.SelectedIndex+1
您使用的播放器是什么?您能简单地挂接一些事件,让您知道何时完成再次更改.URI
吗?我正在使用axWindowsMediaPlayerAlex我尝试了您的代码,但它显示了一些错误错误错误1类中的无效标记“void”,struct,或接口成员声明D:\.net\WindowsApplication1\WindowsApplication1\Form3.cs 52 15 WindowsApplication1当我将async更改为private时,它显示错误1方法“GetValue”没有重载接受“1”参数D:\.net\WindowsApplication1\WindowsApplication1\Form3.cs 45 43 WindowsApplication1,并且在等待的行上也有错误..什么错误?你是否通过表单的设计器添加了播放器?好的,我将发布整个文件内容。可能有一些不匹配的偏执。好的,发布了Form3.cs
的代码,还添加了Form1.cs
的代码。Alex该代码不适用于我bro..System.Linq和System.Threading显示名称空间错误..我想告诉大家我正在使用visual studio 2005进行此操作。这是一个问题吗?
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();
}
}
}
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();
}
}
}
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();
}
}
}