最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - How to use SWFObject correctly - Stack Overflow

matteradmin1PV0评论

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:

/{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?

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./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?

Share Improve this question edited May 23, 2017 at 12:03 CommunityBot 11 silver badge asked Jun 7, 2012 at 10:28 pilaupilau 6,7534 gold badges59 silver badges70 bronze badges 2
  • After SWFObject has embedded the player, inspect the DOM node and check that allowscriptaccess="always" is being set correctly. – user8710 Commented Jun 10, 2012 at 8:17
  • It was being set correctly... I got it working, and will add an answer with the correct code :) – pilau Commented Jun 10, 2012 at 10:50
Add a ment  | 

1 Answer 1

Reset to default 3

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./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)
}
Post a comment

comment list (0)

  1. No comments so far