在特定时间启动和停止视频Windows Media Player

时间:2013-05-19 11:45:15

标签: c#

我正在使用Windows Media Player对象在我的C#winforms项目中播放视频。

 VideoPlayer.URL = "C:\test.avi";

我的test.avi持续时间是12秒。我想在4到8秒之间播放。

我可以从4秒开始播放视频,如下所示;

VideoPlayer.Ctlcontrols.currentPosition = 4

那么我怎样才能在播放视频后的第8秒停止播放视频?

1 个答案:

答案 0 :(得分:1)

您可以使用计时器执行此操作:

private Timer tmrWmpPlayerPosition;
private TimeSpan StopPosition;

private void btn_Click(object sender, EventArgs e)
{
    wmpPlayer.Ctlcontrols.currentPosition = 4;
    StopPosition=TimeSpan.Parse("00:20:20");
    StopWmpPlayerTimer();
    StartWmpPlayerTimer();
}

private void tmrWmpPlayerPosition_Tick(object sender, EventArgs e)
{
    if ((Convert.ToInt32(StopPosition.TotalSeconds) != Convert.ToInt32(wmpPlayer.Ctlcontrols.currentPosition))) return;
    wmpPlayer.Ctlcontrols.pause();
    StopWmpPlayerTimer();
}

private void StartWmpPlayerTimer()
{
    tmrWmpPlayerPosition = new Timer();
    tmrWmpPlayerPosition.Tick += new EventHandler(tmrWmpPlayerPosition_Tick);
    tmrWmpPlayerPosition.Enabled = true;
    tmrWmpPlayerPosition.Interval = 1000;
    tmrWmpPlayerPosition.Start();
}

private void StopWmpPlayerTimer()
{
    if (tmrWmpPlayerPosition != null)
        tmrWmpPlayerPosition.Dispose();
    tmrWmpPlayerPosition = null;
}