views:

40

answers:

1

Hi,

I am trying to create a custom media player in Silverlight. I am working on the Progress Bar. I want the progress bar to display the current Download Progress as well as the Current Position of the mediaelement while it is playing.

To do this I have a Progress Bar to display the download progress and a Slider laid over top to display the current position.

I set the value for both as a percentage out of 100.

For example:

ProgressBar.Value = mediaelement.downloadprogress

Slider.Value = (mediaelement.Position.TotalMilliseconds) / (mediaelement.NaturalDuration.TimeSpan.TotalMilliseconds)

The problem is the Slider.Value becomes larger than the ProgressBar.Value. How is this possible? How can I be playing the video at a farther position than what has been downloaded?

Any advice on how to get these to sync up properly?

Thanks.

+1  A: 

Its possible because video stream compression algorithms do not result in byte counts proportional to time. Take for example a 300MB video file that runs for say 60 minutes it does not automatically follow that 30 minutes into the file would be that the 150MB point.

If the first part of video is relatively "quite" its likely it will compress well whereas busier sections later may not compress so well. As a result its possible to have downloaded only a small percentage of the file size and yet have played a larger percentage of its overall playing time.

Edit:

So how do I get them to sync up?

You fudge it by limiting the slider to minimum of either the download progress or time.

Slider.Value = Math.Min(mediaelement.Position.TotalMilliseconds / mediaelement.NaturalDuration.TimeSpan.TotalMilliseconds, mediaelement.downloadprogress);
AnthonyWJones
Wow. That makes perfect sense when you think about it like that. The start of the video is pretty much all black so it would take no time to download a significant amount of time. So how do I get them to sync up?
Hunter
@Hunter: See my edit
AnthonyWJones