I am using SWFObject to embed YouTube videos into our website. There are many links to videos in one page and each link clears a wrapper div, then loads a new embed into it, with this code:
$('a.video-link').each(function () {
$(this).on('click', function(e) {
e.preventDefault();
if ($('#video-wrap').has('object').length == 0) {
var params = { allowScriptAccess: 'always', allowFullScreen: 'true' };
var atts = { id: 'ytapiplayer' };
swfobject.embedSWF($(this).attr('href'), 'ytapiplayer', '1000', '562', '8', null, null, params, atts);
$('#video-wrap').show();
} else {
$('#video-wrap').find('object').remove();
$(this).trigger('click');
}
});
});
These are the Youtube links I am using for each embed:
http://www.youtube.com/v/{Youtube ID}?hl=en_US&rel=0&hd=1&border=0&version=3&fs=1&autoplay=1&autohide=1&enablejsapi=1&playerapiid=ytapiplayer
Then, here is the onYouTubePlayerReady() event handler:
function onYouTubePlayerReady(id) {
console.log('youtube player is ready.');
var ytplayer = document.getElementById('ytapiplayer');
ytplayer.addEventListener('onStateChange', 'onYouTubePlayerStateChange');
ytplayer.addEventListener('onError', 'onYouTubePlayerError');
}
All videos load fine, however the onYouTubePlayerReady is never hit!
I have tried solutions from here and here but nothing worked :(
Please help me solve this problem. The ultimate goal is to get the Youtube API to work.
Thanks.
EDIT: I tried to play with the code, make sure all names are correct, separate into different script tags and/or .js file, load it on the beginning, inside document.ready(), still, onYouTubePlayerReady is not firing. What do you think?
Here is the working code:
Executing SWFObject on each video link:
$('a.video-link').on('click', function(e) {
e.preventDefault();
// SWFObjects loads a video object into div with ID ytapiplayer.
// If the wrapper div already contains a video we need to remove it first:
if ($('#video-wrap').has('object').length == 0) {
var params = { allowScriptAccess: 'always', allowFullScreen: 'true' };
var atts = { id: 'ytapiplayer' };
swfobject.embedSWF($(this).attr('href'), 'ytapiplayer', '1000', '562', '8', null, null, params, atts);
$('#video-wrap').show();
} else {
$('#video-wrap').find('object').remove();
$(this).trigger('click');
}
});
The YouTube link with API values:
http://www.youtube.com/v/' + data.YoutubeLink + '?hl=en_US&rel=0&hd=1&border=0&version=3&fs=1&autoplay=1&autohide=1&enablejsapi=1&playerapiid=ytapiplayer
And the SWFObject event handlers I put this code in a separate .js file, loaded before the code executing SWFObject. I don't know if it's necessary, but it's working anyway:
function onYouTubePlayerReady(id) {
// We need the actual DOM element for this, if we want to use more advanced API.
// This is because addEventListener activates the API.
var ytplayer = $('#ytapiplayer').get(0);
ytplayer.addEventListener('onStateChange', 'onYouTubePlayerStateChange'); // onYouTubePlayerStateChange(newState)
ytplayer.addEventListener('onError', 'onYouTubePlayerError'); // onYouTubePlayerError(errorCode)
}