Click here to Skip to main content
14,974,887 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have made a WPF Media Player that has SeekBar. I have following code to seek to different position. This code has been written under Slider.ValueChanged event.

mePlayerMain.Position = TimeSpan.FromSeconds(sliderSeekBar.Value);


I have also timer control to control the progress of the seek bar in each tick. I have written following code in MediaElement.MediaOpened

if (mePlayerMain.NaturalDuration.HasTimeSpan)
            {
                TimeSpan ts = new TimeSpan();
                ts = mePlayerMain.NaturalDuration.TimeSpan;
                sliderSeekBar.Maximum = ts.TotalSeconds;
                sliderSeekBar.SmallChange = 1;
            }
            timer.Start();

and for each timer tick, I update the seekbar position
sliderSeekBar.Value = mePlayerMain.Position.TotalSeconds;


But my problem is whenever a media file is playing on the player, media file is disrupted for few seconds and then it plays again. Everythings fine but media file is not smoothly played. Please help me.
Posted
Updated 29-Oct-11 23:16pm
v2
Comments
shijuse 27-Oct-11 1:41am
   
http://www.codeproject.com/KB/WPF/WPF_Media_Player.aspx
For ref....

1 solution

You are almost there.

The problem is that everytime the timer ticks you update the slider value, which causes your slider value changed handler to fire, which sets the position on the video, causing it to skip.

Use a variable to suppress the code in the slider value changed handler before you update your slider value in the tick handler, like so:

C#
bool suppressMediaPositionUpdate = false;

void TimerTickHandler(object sender, EventArgs e)
{
   suppressMediaPositionUpdate = true;
   sliderSeekBar.Value = mePlayerMain.Position.TotalSeconds;
}


void SliderValueChangedHandler(object sender, EventArgs e)
{
   if(suppressMediaPositionUpdate)
   {   
      suppressMediaPositionUpdate = false;
   }
   else
   {
      mePlayerMain.Position = TimeSpan.FromSeconds(sliderSeekBar.Value);
   }
}


-----

Sorry about the formatting, wrote this from my phone.
EDIT: fixed the formatting :)
   
v3
Comments
Jyoti Choudhury 29-Oct-11 6:19am
   
Thank You Mike. It works absolutely fine. Thanks for your solution.
[no name] 30-Oct-11 6:21am
   
My 5!
Espen Harlinn 30-Oct-11 10:34am
   
Excellent :)
shijuse 31-Oct-11 2:33am
   
my 5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900