Embedding and programmatically controlling multiple YouTube videos
by
, 12-02-2014 at 08:09 PM (19497 Views)
We can embed and programmatically control multiple YouTube videos by putting onloads like the following in (existing) iframes:
onload="player1=new YT.Player(this)"
onload="player2=new YT.Player(this)"
etc.
The names for the players (here: player 1 and player2) are arbitrarily chosen. They must be used for selecting the video for which we want to execute javascript (video) code. For instance, player2.playVideo() means that the video associated with player2 must start playing etc .
This will only work if we have this in the head:
<script src="http://www.youtube.com/player_api"></script>
and if the video-url in the iframe contains html5=1 (IE-requirement) and enablejsapi=1.
Demos and more info here.
EDIT:
I delved a bit further into the matter and found a way to not only apply javascript to the videos loaded in an existing (HTML) iframe, but also to their different states, see this and comment#2.