Skip to content

Instantly share code, notes, and snippets.

@hanxue
Created December 8, 2012 09:59
Show Gist options
  • Save hanxue/4239658 to your computer and use it in GitHub Desktop.
Save hanxue/4239658 to your computer and use it in GitHub Desktop.
Stop autoplay on Youtube
try {
var playerelement=document.getElementById("movie_player");
playerelement.mute();
// Possible values are unstarted (-1), ended (0), playing (1), paused (2), buffering (3), video cued (5)
if (playerelement.getPlayerState() == 1) {
if (last_time < 0.01) { // we'll let the player play atleast 0.01 seconds before agreeing that we have a valid time value
last_time = playerelement.getCurrentTime(); // getPlayerState() returns a value, therefore getCurrentTime() should too
}
if ((playerelement.getCurrentTime() - last_time) < 0) { // oops, timetravel!
last_time = playerelement.getCurrentTime();
} else if ((playerelement.getCurrentTime() - last_time) < 0.01) { // before agreeing with the youtube player that the video is playing, let's check that the timecode actually changes
// we'll bid our time for one more cycle to get a second sample
} else { // timestamps did not match.. so let's make it stop
playerelement.pauseVideo();
if ( window.location.href.indexOf("#t=") == -1 ) {
playerelement.seekTo(0,0);
}
zombieguard = 3; //here we define the "lives" of the stop-loop, keeping it on for this many cycles to make sure the video stays stopped
}
} else if (last_time >= 0.01 && playerelement.getPlayerState() == 2) {
if (zombieguard > 0) {
zombieguard -= 1; // loop has one less lives
} else {
playerelement.unMute();
clearInterval(tubestoploop); // we're done.. commit suicide
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment