Created
January 6, 2015 23:33
-
-
Save cjwebdev/a68b060039bc22016d01 to your computer and use it in GitHub Desktop.
Video: Embed YouTube with external controls
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
<iframe id="video" src="//www.youtube.com/embed/FKWwdQu6_ok?showinfo=0&rel=0&wmode=opaque&autohide=1&autoplay=1&enablejsapi=1&html5=1" frameborder="0" allowfullscreen></iframe> | |
<script> | |
// http://css-tricks.com/play-button-youtube-and-vimeo-api/ | |
// https://developers.google.com/youtube/iframe_api_reference | |
// global variable for the player | |
var player; | |
// this function gets called when API is ready to use | |
function onYouTubePlayerAPIReady() { | |
// create the global player from the specific iframe (#video) | |
player = new YT.Player('video', { | |
events: { | |
// call this function when player is ready to use | |
'onReady': onPlayerReady | |
} | |
}); | |
} | |
function onPlayerReady(event) { | |
// bind events | |
var playButton = document.getElementById("play-button"); | |
playButton.addEventListener("click", function() { | |
player.playVideo(); | |
}); | |
var pauseButton = document.getElementById("pause-button"); | |
pauseButton.addEventListener("click", function() { | |
player.pauseVideo(); | |
}); | |
} | |
// Inject YouTube API script | |
var tag = document.createElement('script'); | |
tag.src = "//www.youtube.com/player_api"; | |
var firstScriptTag = document.getElementsByTagName('script')[0]; | |
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment