16

我知道在参数和 url 中使用 autoplay : 0 。问题是当我使用 loadVideoByID() 函数时。初始视频似乎总是不会自动启动。但是当我加载新视频时,新视频会自动启动。我也不希望那个新的自动启动

$(document).ready(function() {
var player;
window.onYouTubePlayerAPIReady = function() {
    player = new YT.Player('player', {
        height : '390',
        width : '640',
        videoId : 'JW5meKfy3fY',
        playerVars : {
            'autoplay' : 0,
            'rel' : 0,
            'showinfo' : 0,
            'egm' : 0,
            'showsearch' : 0,
            'controls' : 0,
            'modestbranding' : 1,
        },
        events : {
            'onReady' : onPlayerReady,
            'onStateChange' : onPlayerStateChange
        }
    });

};

window.onPlayerReady = function(event) {
    //event.target.playVideo();
    loadNewVid("bHQqvYy5KYo");
};

window.onPlayerStateChange = function(event, element) {
    //When the video has ended
    if (event.data == YT.PlayerState.ENDED) {
        //Get rid of the player
        element.style.display = "none";
    }
};

function loadNewVid(vidID){
    player.loadVideoById(vidID, "large");

}

});

4

2 回答 2

35

根据定义,loadVideoById()加载并播放视频。您要使用的是cueVideoById(),它将准备它但等待命令实际播放。

https://developers.google.com/youtube/js_api_reference#cueVideoById

于 2013-11-18T19:01:41.077 回答
0

我认为这篇文章已经很老了,但无论如何我都会分享我的方法,以防有人偶然发现它,就像我刚才所做的那样:

由于无法调用loadVideoById和阻止自动播放,我建议destroy()在播放器上调用该方法并使用新 ID 重新实例化它。

让我们这样说:

$(document).ready(function() {
var player;
var playerParams = {
    height : '390',
    width : '640',
    videoId : 'JW5meKfy3fY',
    playerVars : {
        'autoplay' : 0,
        'rel' : 0,
        'showinfo' : 0,
        'egm' : 0,
        'showsearch' : 0,
        'controls' : 0,
        'modestbranding' : 1,
    },
    events : {
        'onReady' : onPlayerReady,
        'onStateChange' : onPlayerStateChange
    }
}

window.onYouTubePlayerAPIReady = function() {
    player = new YT.Player('player', playerParams);

};

window.onPlayerReady = function(event) {
    //event.target.playVideo();
    loadNewVid("bHQqvYy5KYo");
};

window.onPlayerStateChange = function(event, element) {
    //When the video has ended
    if (event.data == YT.PlayerState.ENDED) {
        //Get rid of the player
        element.style.display = "none";
    }
};

function loadNewVid(vidID){
    player.destroy();
    playerParams.videoId = vidID;
    player = new YT.Player('player', playerParams);

}
});

我希望这可能会有所帮助

于 2016-04-01T10:08:14.563 回答