Reputation: 16481
I am trying to create a youtube video using the youtube iframe api along with a constructor function I'm working on but I've hit a stumbling block. At the moment in my Player function I have created some default properties and then I pass some new properties into my new Object in order to create a player by extending the default and new properties. My problem now is that I'm not sure how I actually initialise the youtube video? Im not sure where player = new YT.Player('player', {
should go?
Here is the JS im working on and the jsFiddle http://jsfiddle.net/kyllle/6zuh5/7/
function Player(options) {
var $player = $(options.id);
var defaults = {
height: '100',
width: '200',
videoId: 'u1zgFlCw8Aw',
events: {
'onReady': onPlayerReady
},
playerVars: {
modestbranding: 0,
controls: 0, //remove controls
showinfo: 0,
enablejsapi : 1,
iv_load_policy: 3
}
};
var combinedOptions = _.extend(defaults, options);
console.log('Combined Options', combinedOptions);
return {
pause: function () {
$player.pauseVideo();
},
seek: function () {
//$player.seekTo();
},
destroy: function () {
$player.destroy();
},
changeVideo: function () {
$player.stopVideo();
}
}
};
function onPlayerReady() {
console.log('player fired');
}
var myPlayer = new Player({
id: '#divId',
autoPlay: true,
videoId: 'asdadads'
});
Upvotes: 4
Views: 2356
Reputation: 501
A working example of a Player class which is used to create and control separate players.
HTML:
<div id="divId1"></div>
<a href="#" id="play1">Play</a>
<a href="#" id="pause1">Pause</a>
<a href="#" id="stop1">Stop</a>
<br>
<div id="divId2"></div>
<a href="#" id="play2">Play</a>
<a href="#" id="pause2">Pause</a>
<a href="#" id="stop2">Stop</a>
<script>
var tag = document.createElement('script');
tag.src = "//www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
</script>
Javascript
var Player = (function(){
//private static
var defaults = {
height: '100',
width: '200',
videoId: 'u1zgFlCw8Aw',
events: {},
playerVars: {
modestbranding: 0,
controls: 0, //remove controls
showinfo: 0,
enablejsapi : 1,
iv_load_policy: 3
}
};
var constructor = function(options){
this.options = _.extend(defaults, options);
this.pause = function(event){
event.target.pauseVideo()
}
if(this.options.autoPlay){
this.options.events['onReady'] = function(event){
event.target.playVideo()
}
}
this.player = new YT.Player(this.options.id,this.options)
//pause on click
$(this.options.pauseID).bind('click',function(event){
this.player.pauseVideo()
}.bind(this))
//play on click
$(this.options.playID).bind('click',function(event){
this.player.playVideo()
}.bind(this))
//stop on click
$(this.options.stopID).bind('click',function(event){
this.player.stopVideo()
}.bind(this))
}
return constructor;
})() //function(){
$(document).ready(function(){
var myPlayer = new Player({
id: 'divId1',
pauseID:'#pause1',
playID:'#play1',
stopID:'#stop1',
autoPlay: false,
videoId: 'oe_mGl1f4xs'
});
var myPlayer2 = new Player({
id: 'divId2',
pauseID:'#pause2',
playID:'#play2',
stopID:'#stop2',
autoPlay: false,
videoId: 'u1zgFlCw8Aw'
});
})
The code to create the Player class is based on this question.
Upvotes: 0
Reputation: 1168
Add a function called init
after the changeVideo
function. Then after instantiating Player you would call init on that new instance. (i.e. myPlayer.init()
)
Here is an example of what I'm thinking: I omitted underscore and instead used jQuery's $.extend
function just so I had less to mess with:
<html>
<head>
<title>Video Player</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
function Player(options) {
var defaults = {
height: '100',
width: '200',
videoId: 'u1zgFlCw8Aw',
events: { 'onReady': null},
playerVars: {
modestbranding: 0,
controls: 0, //remove controls
showinfo: 0,
enablejsapi : 1,
iv_load_policy: 3
}
};
var combinedOptions = $.extend(defaults, options);
console.log('Combined Options', combinedOptions);
return {
player: null,
pause: function () {
this.player.pauseVideo();
},
seek: function () {
//this.player.seekTo();
},
destroy: function () {
this.player.destroy();
},
changeVideo: function () {
this.player.stopVideo();
},
onPlayerReady: function() {
this.player.play();
},
init: function() {
this.player = new YT.Player(combinedOptions.id, {
height: combinedOptions.height,
width: combinedOptions.width,
videoId: combinedOptions.videoId,
events: {
'onReady': this.onPlayerReady,
'onStateChange': null
}
});
}
}
};
var myPlayer;
function onYouTubeIframeAPIReady() {
myPlayer = new Player({
id: 'divId',
autoPlay: true,
videoId: 'NeGe7lVrXb0'
});
myPlayer.init();
}
$(document).ready(function() {
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
});
</script>
</head>
<body>
<div id="divId"></div>
</body>
</html>
Upvotes: 4
Reputation: 465
place it where you want your video to be:
<div width="200" height="100" onclick="loadVideo();"></div>
place itat the end of page:
<script>
function loadVideo() { // 2. This code loads the IFrame Player API code asynchronously.
var tag = document.createElement('script');
tag.src = "http://www.youtube.com/player_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag); }
// func loadVideo
// 3. This function creates an <iframe> (and YouTube player)
// after the API code downloads.
var player;
function onYouTubePlayerAPIReady() { player = new YT.Player('player', { height: '100', playerVars: { 'autoplay': 1, rel: 0 }, width: '200', videoId: 'mXtJ9BbeGB4', events: { 'onReady': onPlayerReady } }); }
// 4. The API will call this function when the video player is ready.
function onPlayerReady(event) { event.target.playVideo(); }
</script>
Upvotes: 0