Search code examples
youtube-apihtml5-videohtml5-audiobufferingyoutube-javascript-api

Media pauses because of slow buffering, and then never recovers from it. How to handle this situation?


I think we all run into this as a user as well. For example, when I'm playing a Youtube video, and the connection is slow, it will not play until enough content has been buffered. But sometimes even after the content arrived it won't resume playing.

Most of the times when this happens I just click once somewhere on the player bar and it will resume.

Now, I think I've run into this situation while programming with HTML5 Audio elements and Youtube APIs. Most of the times they work well, but when the connection is spotty, it will just stop and never resume even after content has arrived. Is there a way to get around this problem? Thank you!


Solution

  • HTML5 Media Elements trigger a lot of useful events that might help you solve this problem. I would consider starting a timer when the waiting event fires (which indicates buffering) then periodically attempt to resume playing the track until canplay fires.

    You could also call load instead of play then wait for canplaythrough fires (which indicates that media can play continuously without needing to pause for buffering).

    Of course it's possible that the YouTube API prevents these events from propagating to the containing page, but it's worth a try.