在 youtube javascript api 中禁用自动播放

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20054930/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 17:35:00  来源:igfitidea点击:

Disable autoplay in youtube javascript api

javascriptjqueryyoutube-apiyoutube-javascript-apiyoutube-data-api

提问by Taf Munyurwa

I know about using autoplay : 0 in params and in url. The problem is when I use the loadVideoByID() function. THe initial video seems to always not autostart. but the moment I load in new video that new one autostarts. I dont want that new one to autostart either

我知道在 params 和 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");

}

});

});

回答by jlmcdonald

By definition, loadVideoById()loads AND plays the video. What you want to use is cueVideoById(), which will prepare it but wait for a command to actually play.

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

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

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

回答by Superstooge

I reckon this post is very old, but I'd share my approach anyway, in case someone stumbles upon it, as I just did:

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

Since there is no way of calling loadVideoByIdand prevent autoplay, I would suggest calling the destroy()method on the player and re-instanciate it, with the new ID.

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

Let's say something like:

让我们说一下:

$(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);

}
});

I hope this may help

我希望这可能会有所帮助