Tuesday, July 24, 2012

Know What’s Loaded?

If you’ve ever developed a custom playback experience based on our Player API, you’ve probably used the getVideoBytesLoaded() and getVideoBytesTotal() methods to determine how much the current video is locally buffered and the size of the entire video. After obtaining this information, the next logical step is to divide the number of bytes loaded by the total number of bytes to obtain the fraction of the video that’s been buffered, which you can then display in your custom user interface.

Instead of taking this two-step approach to calculate how much of a video has buffered, we encourage you to use the new getVideoLoadedFraction() method. It will return a numerical value between 0.0 and 1.0 that represents the same fraction obtained by dividing getVideoBytesLoaded() by getVideoBytesTotal(). While those two legacy methods still work today, they are now officially deprecated, as per the terms of our deprecation policy.

getVideoLoadedFraction() allows us to expose more accurate information to your code about the state of the video that’s currently playing across a wider range of playback scenarios. When HTML5 <video> playback was used in the <iframe> Player, for instance, we previously had to return “fake” values for getVideoBytesLoaded() and getVideoBytesTotal(), as the true byte counts weren’t directly exposed via the <video> element. The value returned by getVideoLoadedFraction() should be correct for that and other playback scenarios that we roll out in the future.

If you have any questions about the new method or anything else related to the YouTube APIs, please let us know in the developer forum.

Cheers,
Jeffrey Posnick, YouTube API Team