An experiment with showing videos and controlling them via JavaScript. Learned that Vimeo (as of the time this experiment was created) does not allow JavaScript control, whereas YouTube does.
This experiment was created to test JavaScript capabilities regarding building a video-based site for a client..
25–26 Oct 2008.
Lots of troubleshooting and getting the right syntax. Different browsers seem to interpret slightly differently, so it’s somewhat hard to tell what’s happening, too.
Here’s the code. Please link back here if you’re using it.
Put this HTML in the body tag.
<div class='vPlayers' >
<div id='video1Player' >
</div>
</div>
<div class='vPlayers' >
<div id='video2Player' >
</div>
</div>
Place this JavaScript at the end of body tag, after the above HTML.
//settings for Video 1
var params1 = { allowScriptAccess: "always" };
var atts1 = { id: "video1" };
swfobject.embedSWF("http://www.youtube.com/apiplayer?hl=en%26fs=1%26rel=0%26enablejsapi=1%26playerapiid=ytplaye1","video1Player", "425", "344", "8", null, null, params1, atts1);
//settings for Video 2
var params2 = { allowScriptAccess: "always" };
var atts2 = { id: "video2" };
swfobject.embedSWF("http://www.youtube.com/apiplayer?hl=en%26fs=1%26rel=0%26enablejsapi=1%26playerapiid=ytplayer2","video2Player", "425", "344", "8", null, null, params2, atts2);
//The text that will change depending on "playing" or "paused"
var p10PlayPause=document.getElementById("p10PlayPause");
//Manual switch for determining whether the videos are playing or not.
var videosPlaying=false;
//Change the text based on the switch state.
function applyPlayPause(){
if (videosPlaying==false){
p10PlayPause.innerHTML="Play";
} else if (videosPlaying==true){
p10PlayPause.innerHTML="Pause";
}
}
//Play/Pause all videos
function p10PlayPauseVideo(){
if (videosPlaying==false){
ytplayer1.playVideo();
ytplayer2.playVideo();
videosPlaying=true;
applyPlayPause();
} else if (videosPlaying==true){
ytplayer1.pauseVideo();
ytplayer2.pauseVideo();
videosPlaying=false;
applyPlayPause();
}
}
//Assign the video embedding to the player, then cue the video
function onYouTubePlayerReady(playerId){
ytplayer1=document.getElementById("video1");
ytplayer2=document.getElementById("video2");
ytplayer1.cueVideoById("OlPzNJSOG9U",0);
ytplayer2.cueVideoById("SlTvSUCCqPo",0);
}
function fnToLoad(){
applyPlayPause();
}
onload=fnToLoad();