Silverlight MediaElement progress
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 overlaid 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.
It's 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 "quiet", it's likely it will compress well whereas busier sections later may not compress so well. As a result it's 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);
精彩评论