在youtube api中嵌入多个视频导致错误



我使用几个HTMLLoader在Air应用程序中嵌入youtube视频(每个加载器一个),当我添加两个以上时,它会导致API中所有玩家超过第二个的错误。玩家似乎工作得很好,但事件从未被调用,玩家。pauseVideo未定义

TypeError: Result of expression 'b' [undefined] is not an object.
at http://s.ytimg.com/yts/jsbin/www-embed-player-vflJ4hakx.js : 206
at http://s.ytimg.com/yts/jsbin/www-embed-player-vflJ4hakx.js : 205
at http://s.ytimg.com/yts/jsbin/www-embed-player-vflJ4hakx.js : 204
at http://s.ytimg.com/yts/jsbin/www-embed-player-vflJ4hakx.js : 199

我的嵌入代码:

function SetupYoutubePlayer(divName, videoID) {
    _videoID = videoID;
    _divName = divName;
    var tag = document.createElement('script');
    tag.src = "https://www.youtube.com/iframe_api";
    var firstScriptTag = document.getElementsByTagName('script')[0];
    firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
}
var _videoID;
var _divName;
var player;
function onYouTubeIframeAPIReady() {
    player = new YT.Player(_divName, {
        height: '100%',
        width: '100%',
        wmode: 'opaque',
        autohide: '1',
        modestbranding: '1',
        frameborder: "0",
        showinfo: '0',
        videoId: _videoID,
        playerVars: { 
            'wmode': 'opaque', 
            'rel': 0, 
            'autohide': 1,
            'modestbranding': 1,
            'frameborder': 0,
            'showinfo': 0,
        },
        events: {
            'onStateChange': onPlayerStateChange
        }
    });
}

看起来您重复调用以下行:

var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

这些只需要执行一次,以拉入YT.Player定义和相关的API。如果你确保只执行一次,事情会像预期的那样工作吗?

相关内容

  • 没有找到相关文章

最新更新