Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable autoplay in youtube javascript api

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

}

});

like image 515
Taf Munyurwa Avatar asked Nov 18 '13 18:11

Taf Munyurwa


People also ask

How do I permanently turn off autoplay on YouTube?

1. On the Home tab of the YouTube app, tap your profile picture, then tap Settings. 2. Tap Autoplay, then tap the autoplay switch to the off position to turn off the feature.

How do I turn off autoplay on YouTube app?

Android & iOS Tap anywhere on the video to reveal the controls. The autoplay slider is located along the top, next to the Cast, Subtitles/CC, and Settings buttons. If it has a play triangle in the middle and the slider is on the right, then Autoplay is on.


1 Answers

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

like image 61
jlmcdonald Avatar answered Sep 30 '22 08:09

jlmcdonald