Reputation: 1266
I am using Windows Media Player object for playing video in my C# winforms project.
VideoPlayer.URL = "C:\test.avi";
My test.avi duration is 12 seconds. I would like to play that between 4 and 8 seconds.
I can start video from 4 second as below;
VideoPlayer.Ctlcontrols.currentPosition = 4
So how can i stop the video 8th seconds after playing video?
Upvotes: 4
Views: 12404
Reputation: 15563
You can use a timer to do so:
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;
}
Upvotes: 2