Disable autoplay in youtube javascript api

自闭症网瘾萝莉.ら 提交于 2019-12-22 02:20:55

问题


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

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

}

});


回答1:


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.

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




回答2:


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 loadVideoById and prevent autoplay, I would suggest calling the destroy() method on the player and re-instanciate it, with the new 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



来源:https://stackoverflow.com/questions/20054930/disable-autoplay-in-youtube-javascript-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!