pausing a video after every 10s in c#

家住魔仙堡 提交于 2019-12-13 04:28:31

问题


what i want to do is pause a video after every 10s the video should pause after ever 10s till the video ends

the code given below gives unexpected results the video pauses fine for the firs time (i.e after 10s) but when i play again it should pause after 10s but in my case it pauses randomly sometimes at 8s,3s 5s and etc what should i do?? please help thanks!!

void PlayClick(object sender, EventArgs e)
{
             VideoControl.Play();
            var dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
            dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
            dispatcherTimer.Interval = new TimeSpan(0, 0, 10);
            dispatcherTimer.Start();
 }

 private void dispatcherTimer_Tick(object sender, EventArgs e)
        {

            VideoControl.Pause();
        }

回答1:


  1. Add this in your dispatcherTimer_Tick-Method:

    dispatcherTimer.Stop();
    
  2. Move the following part into the constructor:

    dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
    dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
    dispatcherTimer.Interval = new TimeSpan(0, 0, 10);
    
  3. Make the DispatcherTimer a global variable.

EDIT: Thats how it should look like:

    class MyClass
    {
        private DispatcherTimer _dispatcherTimer; //now your dispatcherTimer is accessible everywhere in this class

        public MyClass()
        {
            _dispatcherTimer = new DispatcherTimer();
            _dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
            _dispatcherTimer.Interval = new TimeSpan(0, 0, 10);
        }

        void PlayClick(object sender, EventArgs e)
        {
            VideoControl.Play();
            _dispatcherTimer.Start();
        }

        void dispatcherTimer_Tick(object Sender, EventArgs e)
        {
            _dispatcherTimer.Stop();
            VideoControl.Pause();
        }
    }



回答2:


Bring the declaration of the timer out into a private class variable, move a couple lines to the constructor of the class, and stop the timer in the Tick handler.

The reason you don't want to keep creating the timer is because there are unmanaged resources involved with a timer and so you're closing that loop.

private dispatcherTimer = new System.Windows.Threading.DispatcherTimer(); 

ctor
{
    dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick); 
    dispatcherTimer.Interval = new TimeSpan(0, 0, 10); 
}

void PlayClick(object sender, EventArgs e) 
{ 
    VideoControl.Play(); 
    dispatcherTimer.Start(); 
} 

private void dispatcherTimer_Tick(object sender, EventArgs e) 
{ 
    dispatchTimer.Stop();
    VideoControl.Pause(); 
} 



回答3:


Try with following code in Tick event:

private void dispatcherTimer_Tick(object sender, EventArgs e)
    { 
        (sender as DispatcherTimer).Stop();
        VideoControl.Pause();
    }

You can make dispatcherTimer object outside Playclick event and only put Start() method inside PlayClick event in following way:

var dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
public Form1() //// form constructor where you are handling these all event....
{
     dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
     dispatcherTimer.Interval = new TimeSpan(0, 0, 10);
}

void PlayClick(object sender, EventArgs e)
{
            VideoControl.Play();
            dispatcherTimer .Start();
}


来源:https://stackoverflow.com/questions/12607440/pausing-a-video-after-every-10s-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!