An experiment in scripting the YouTube player with external controls and the IFrame API.
I have some ideas for how I’d like to be able to embed YouTube, but they hinge on the ability to control a YouTube player and also get feedback on current player position. this is a quick test to see if all these possibilities are realistic.
A quick search yeilded the YouTube Player API Reference for iframe Embeds, which appears to be the right medicine. So that’s what I’m going to try..
- external play trigger
- mark times
- play section between marked times
- loop between marked times
Some Useful Features of the API
onYouTubeIframeAPIReady is a function that must be implemented. It is called by the API when loaded and ready.
Playback controls include
Mark and Loop
getCurrentTime() function gets the appropriate video position for a “Mark” operation.
Rewind/set to play from a specific time mark can be achieved with the
It seems there’s no direct way to directly combine an end time with
To play a specific time window (from
endSeconds) appears only to be directly supported
cueVideoById functions. But these entail reloading the entire video,
so not ideal for performing a loop between start/end in an already-loaded video.
So the approach that appears best is:
getCurrentTime()to mark start/end points
- start a loop with
seekTo(), and use
setTimeoutto fire at approximately the desired end point.
- at the end point, either stop the video or loop back with another
seekTo()depending on whether infinite looping is desired.
Play, Pause and Stop buttons have their expected effect.
A Mark button is use to set/reset the start and end markers.
- when the second mark has been added, it will automatically replay the video between the two marks
- if the ‘loop’ checkbox is selected, the video will loop between the two marks