Last active
May 21, 2023 01:03
-
-
Save thejsj/6576250 to your computer and use it in GitHub Desktop.
A short script to integrate the Youtube Player API with Javascript.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Video = function(id, index){ | |
this.id = id; | |
this.index = index; | |
this.w = Math.random() * 200 + 100; | |
this.h = this.w * (9/16); | |
this.init(); | |
this.volume = 50; | |
this.muted = false; | |
}; | |
Video.prototype.init = function(){ | |
// The video to load. | |
$("<div id='c" + this.id + "'></div>").appendTo("#container"); | |
this.elementId = "video" + this.id; | |
// Lets Flash from another domain call JavaScript | |
var params = { allowScriptAccess: "always" }; | |
// The element id of the Flash embed | |
var atts = { id: this.elementId, class: "video" }; | |
// All of the magic handled by SWFObject (http://code.google.com/p/swfobject/) | |
swfobject.embedSWF("http://www.youtube.com/apiplayer?" + | |
"version=3&enablejsapi=1&playerapiid=player" + this.index, | |
"c" + this.id, this.w, this.h, "9", null, null, params, atts); | |
} | |
Video.prototype.createPlayer = function(){ | |
this.player = document.getElementById(this.elementId); | |
this.player.cueVideoById(this.id); | |
this.player.setVolume(); | |
this.playing = false; | |
} | |
Video.prototype.playPause = function(){ | |
if(this.playing){ | |
this.player.pauseVideo(); | |
this.playing = false; | |
} | |
else { | |
this.player.playVideo(); | |
this.playing = true; | |
} | |
} | |
Video.prototype.muteUnmute = function(){ | |
if(this.muted){ | |
this.unMute(); | |
} | |
else { | |
this.muted(); | |
} | |
} | |
Video.prototype.mute = function(){ | |
this.player.mute(); | |
this.muted = true; | |
} | |
Video.prototype.unMute = function(){ | |
this.player.unMute(); | |
this.muted = false; | |
} | |
Video.prototype.increaseVolume = function(){ | |
this.volume = this.volume + 5; | |
if(this.volume > 100) { | |
this.volume = 100; | |
} | |
this.setVolume(); | |
} | |
Video.prototype.decreaseVolume = function(){ | |
this.volume = this.volume - 5; | |
if(this.volume < 0) { | |
this.volume = 0; | |
} | |
this.setVolume(); | |
} | |
Video.prototype.setVolume = function(){ | |
this.player.setVolume(this.volume); | |
} | |
Video.prototype.goToMiddle = function(){ | |
var seconds = this.player.getDuration() / 2; | |
this.goToSpecificTime(seconds); | |
} | |
Video.prototype.goToStart = function(){ | |
this.goToSpecificTime(0); | |
} | |
Video.prototype.goToRandom = function(){ | |
var seconds = parseInt(this.player.getDuration() * Math.random()); | |
this.goToSpecificTime(seconds); | |
} | |
Video.prototype.goToSpecificTime = function(seconds){ | |
this.player.seekTo(seconds); | |
} | |
Video.prototype.resize = function(){ | |
this.w = this.player.width = Math.random() * 200 + 100; | |
this.h = this.player.height = this.w * (9/16); | |
} | |
// Start Function | |
var videos = []; | |
var video_ids = ["2IaNleJa8JQ", | |
"wVHSnymzTLI", | |
"ogfD1AzHIOg", | |
"EKiS9Qsbia4", | |
"Snkh4aXgySg", | |
"hjBoPC5fDIU", | |
"V_AXevRPFhY", | |
"OkVti5vHnP8", | |
"dqdjnfmzmYY", | |
"mGhlBZ4t-Ok", | |
"PqbmBM-z72s" | |
]; | |
for(var i = 0; i < video_ids.length; i++){ | |
videos.push(new Video(video_ids[i], videos.length)); | |
} | |
function onYouTubePlayerReady(playerId) { | |
var id = parseInt(playerId.replace("player","")) | |
videos[id].createPlayer(); | |
} | |
$("#pause_videos").click(function(){ | |
for(var i = 0; i < videos.length; i++){ | |
videos[i].playPause(); | |
} | |
}); | |
$("#increase_v_all").click(function(){ | |
for(var i = 0; i < videos.length; i++){ | |
videos[i].increaseVolume(); | |
} | |
}); | |
$("#decrease_v_all").click(function(){ | |
for(var i = 0; i < videos.length; i++){ | |
videos[i].decreaseVolume(); | |
} | |
}); | |
$("#mute_all").click(function(){ | |
for(var i = 0; i < videos.length; i++){ | |
videos[i].muteUnmute(); | |
} | |
}); | |
$("#go_to_middle").click(function(){ | |
for(var i = 0; i < videos.length; i++){ | |
videos[i].goToMiddle(); | |
} | |
}); | |
$("#go_to_start").click(function(){ | |
for(var i = 0; i < videos.length; i++){ | |
videos[i].goToStart(); | |
} | |
}); | |
$("#go_to_random").click(function(){ | |
for(var i = 0; i < videos.length; i++){ | |
videos[i].goToRandom(); | |
} | |
}); | |
$("#resize").click(function(){ | |
for(var i = 0; i < videos.length; i++){ | |
videos[i].resize(); | |
} | |
}); | |
$(document).on( "mouseover", ".video", function(){ | |
for(var i = 0; i < videos.length; i++){ | |
if(videos[i].elementId == this.id){ | |
videos[i].unMute(); | |
} | |
else { | |
videos[i].mute(); | |
} | |
} | |
}); | |
$(document).on( "mouseout", ".video", function(){ | |
for(var i = 0; i < videos.length; i++){ | |
videos[i].unMute(); | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment