Skip to content

Instantly share code, notes, and snippets.

@tvpmb
Created August 13, 2013 15:54
Show Gist options
  • Save tvpmb/6222630 to your computer and use it in GitHub Desktop.
Save tvpmb/6222630 to your computer and use it in GitHub Desktop.
Backbone JS View fragment for Youtube IFrame API
playVideo: function() {
this.hidePoster();
if (!YT) {
$('head').append('<script src="//www.youtube.com/iframe_api" type="text/javascript"></script>');
}
if (this.player) {
this.player.loadVideoById(this.videoId);
this.player.playVideo();
} else {
this.setupPlayer();
}
},
showPoster: function() {
$('#videoChooser').show();
$('#videoContainer').hide();
},
hidePoster: function() {
$('#videoChooser').hide();
$('#videoContainer').show();
},
initPlayer:function(){
var _this = this;
_this.player = new YT.Player('videoPlayer', {
height: '100%',
width: '100%',
videoId: _this.videoId,
events: {
'onReady': _this.onPlayerReady,
'onPlaybackQualityChange': _this.onPlayerPlaybackQualityChange,
'onStateChange': _this.onPlayerStateChange,
'onError': _this.onPlayerError
},
playerVars: {
controls: 1,
modestbranding: 1,
autohide: 1,
enablejsapi: 1,
showinfo: 0,
rel: 0,
autoplay: 1
}
});
},
setupPlayer: function() {
var _this = this;
if (YT) {
_this.initPlayer(_this.videoId);
} else {
window.onYouTubeIframeAPIReady = function() {
_this.initPlayer(_this.videoId);
}
}
},
onPlayerReady: function(e) {
},
onPlayerPlaybackQualityChange: function(e) {
},
onPlayerStateChange: function(e) {
if (e.data == "0") {
$('#videoChooser').show();
$('#videoContainer').hide();
}
},
onPlayerError: function(e) {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment