Using Youtube's javascript API with jQuery

前端 未结 5 1098
萌比男神i
萌比男神i 2020-12-08 03:04

I\'m currently trying to use the YouTube API as part of a jQuery plugin and I\'ve run into a bit of a problem.

The way the YT api works is that you load the flash pl

5条回答
  •  执念已碎
    2020-12-08 03:24

    Im Using Jquery SWFobject plugin, SWFobject

    It is important to add &enablejsapi=1 at the end of video

    HTML:

    Jquery:

                 $('#embedSWF').flash({ 
                    swf: 'http://www.youtube.com/watch/v/siBoLc9vxac',
                    params: { allowScriptAccess: "always"},   
                    flashvars: {enablejsapi: '1', autoplay: '0', allowScriptAccess: "always", id: 'ytPlayer' },   
                    height: 450,   width: 385 });
    
    function onYouTubePlayerReady(playerId) {
                    $('#embedSWF').flash(function(){this.addEventListener("onStateChange", "onPlayerStateChange")});
                }
    function onPlayerStateChange(newState) {
                       alert(newState);
                     }
    

    onYouTubePlayerReady must be outside of $(document).ready(function() to get fired

提交回复
热议问题