YouTube has long made it easy to share and embed videos into websites by giving you the HTML code to just copy and paste wherever you like. Over the years, different technologies have been used to play videos on the web, from Flash
object elements to pure HTML5. YouTube's newest API for adding videos to web pages takes care of the technology, automatically choosing the best delivery method on a user by user basis.
- Get a
- Load a video into the player:
videoId, which you can get from the video's URL).
- Play it!
onStateChange events, which will fire when the player starts playing, is paused, and also when buffering.
Even when accounting for buffering, I've found that the player's events can be delayed significantly, or that player actions happen significantly before the events fire. If you are trying to control the player using timers (setTimeout) then you'll be off by a second or more on mobile.
If you want to play a segment of a video (e.g. seeking to time 31.5 seconds and playing for 28.5 seconds) then you can achieve it more accurately using the arguments passed in when you load the video. Use the options