Skip to content

Instantly share code, notes, and snippets.

@yggie
Last active December 29, 2016 06:03
Show Gist options
  • Save yggie/40ad6509c5cf2e85feb8 to your computer and use it in GitHub Desktop.
Save yggie/40ad6509c5cf2e85feb8 to your computer and use it in GitHub Desktop.
YouTube and HTML5 Video Player Embedding With Limited Playable Videos
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<script src="https://code.jquery.com/jquery-2.1.1.js"></script>
</head>
<body>
<video width="500" height="281" controls>
<source src="http://download.wavetlan.com/SVV/Media/HTTP/H264/Talkinghead_Media/H264_test1_Talkinghead_mp4_480x360.mp4" type="video/mp4" />
</video>
<img src="http://www.thinkuknow.co.uk/Global/swf/ThinkUKnow%205_7/nice.jpg" data-video="sc395CdrpFw">
<img src="http://2.bp.blogspot.com/--vOShBApMXo/TmBWxEXiAOI/AAAAAAAAAHs/TN8JCzzlM5k/s1600/niceguy.jpg" data-video="TF4kmztJ4Tg">
<script>
// How did I do this magic? see https://developers.google.com/youtube/iframe_api_reference#Getting_Started
var players = [];
function pauseAllPlayersExcept(player) {
for (var i = 0; i < players.length; i++) {
if (players[i] && players[i] !== player) {
if (players[i] instanceof YT.Player) {
players[i].pauseVideo();
} else {
players[i].pause();
}
}
}
}
$('video').each(function () {
var element = $(this),
index = players.length;
players[index] = element[0]; // get the pure un-jQuery-ified element
element.bind('play', function (e) {
pauseAllPlayersExcept(players[index]);
});
});
function onYouTubeIframeAPIReady() {
$('img').each(function () {
var element = $(this),
index = players.length;
// save a space in the array
players[index] = null;
element.attr('id', 'video-' + index);
element.click(function () {
players[index] = new YT.Player(element.attr('id'), {
height: 390,
width: 640,
videoId: element.data('video'),
events: {
onReady: function (event) {
// equivalent to autoplay=1
event.target.playVideo();
},
onStateChange: function (event) {
if (event.data === YT.PlayerState.PLAYING) {
pauseAllPlayersExcept(players[index]);
}
},
}
});
});
});
}
</script>
<script src="https://www.youtube.com/iframe_api"></script>
</body>
</html>
@jjessica
Copy link

Very nice post here thanks for it I always like and search such topics and everything connected to them.Excellent and very cool idea and the subject at the top of magnificence and I am happy to comment on this topic through which we address the idea of positive re like this.
school branding uk

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment