4

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

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

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

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

VideoPlayer.Ctlcontrols.currentPosition = 4

那么如何在播放视频后第 8 秒停止视频?

4

1 回答 1

2

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

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;
}
于 2014-09-24T05:51:06.593 回答