Desabilite a reprodução automática no youtube api de javascript
-
02-01-2020 - |
Pergunta
Eu sei sobre como usar reprodução automática :0 em params e na url.O problema é que quando eu uso o loadVideoByID() função.A inicial de vídeo parece sempre não iniciar automaticamente.mas o momento que eu carga em novo vídeo que um novo autostarts.Eu não quero que um novo início automático de um
$(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");
}
});
Solução
Por definição, loadVideoById()
carrega E reproduz o vídeo.O que você pretende utilizar é cueVideoById()
, que irá prepará-lo, mas aguarde um comando para jogar de verdade.
https://developers.google.com/youtube/js_api_reference#cueVideoById
Outras dicas
Eu acho que este post é muito antigo, mas que eu iria partilhar a minha abordagem de qualquer maneira, no caso de alguém se depara com ele, como eu fiz:
Como não há nenhuma forma de chamar loadVideoById
e impedir a reprodução automática, sugiro chamar a destroy()
método sobre o jogador e re-instanciate isso, com o novo ID.
Vamos dizer algo como:
$(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);
}
});
Espero que isso possa ajudar