C# Winform WindowsMediaPlayer控件

时间:2023-01-12 19:39:23

要做一个视频无缝切换的程序,所谓无缝就是在一个视频结束时立即开始另一个视频,中间不要有切换的黑屏

实现思路是放两个wmp播放控件,其中每个时刻只有一个在播放,另外一个处于暂停状态,并隐藏

当一个视频播放完后,切换显示另一个视频,同时这个视频隐藏起来并加载下一段视频,视频加载完成后(wmp会有事件通知)暂停

这样就去除了加载时的短暂空挡

有时可能会有键盘鼠标事件影响wmp的状态,所以使用定时器虽然监测wmp的播放状态

private void Form1_Load(object sender, EventArgs e)
{
timer1.Enabled = true;
wmp1.URL = ConfigurationManager.AppSettings["File1Path"];
wmp2.URL = ConfigurationManager.AppSettings["File1Path"];
playnow = ; wmpnow = ;
wmp1.Visible = true;
wmp2.Visible = false; wmp1pause = ;
wmp2pause = ; }
 private bool isFullScreen = false;
private int playnow = ;
private int wmp1pause = ;
private int wmp2pause = ;
private int wmpnow = ; private void timer1_Tick(object sender, EventArgs e)
{
if (wmp1.Ctlcontrols.currentPosition == )
wmp1.Ctlcontrols.play();
else
{
if (wmp1.Ctlcontrols.currentPosition > && wmp1pause== && wmpnow==)
{
if (playnow == )
{
wmp1.Visible = true;
wmp1.settings.volume = wmp2.settings.volume;
wmpnow = ; playnow = ;
wmp2.Visible = false;
wmp2.Ctlcontrols.stop();
wmp2pause = ;
}
else
wmp1.Ctlcontrols.pause();
wmp1pause = ;
}
if (wmp1.currentMedia.duration - wmp1.Ctlcontrols.currentPosition < 0.05)
{
wmpnow = ;
wmp2.Visible = true;
wmp2.settings.volume = wmp1.settings.volume;
wmp2.Ctlcontrols.play(); wmp1.Visible = false;
wmp1.Ctlcontrols.stop();
wmp1.URL = ConfigurationManager.AppSettings["File1Path"];
wmp1pause = ;
}
}
if (wmp2.Ctlcontrols.currentPosition == )
wmp2.Ctlcontrols.play();
else
{
if (wmp2.Ctlcontrols.currentPosition > && wmp2pause == && wmpnow==)
{
if (playnow == )
{
wmp2.Visible = true;
wmp2.settings.volume = wmp1.settings.volume;
wmpnow = ; playnow = ;
wmp1.Visible = false;
wmp1.Ctlcontrols.stop();
wmp1pause = ;
}
else
wmp2.Ctlcontrols.pause();
wmp2pause = ;
}
if (wmp2.currentMedia.duration - wmp2.Ctlcontrols.currentPosition < 0.05)
{
wmpnow = ;
wmp1.Visible = true;
wmp1.settings.volume = wmp2.settings.volume;
wmp1.Ctlcontrols.play(); wmp2.Visible = false;
wmp2.Ctlcontrols.stop();
wmp2.URL = ConfigurationManager.AppSettings["File1Path"];
wmp2pause = ;
}
}
}