In multiple YouTube videos, how do I play on one and pause another?

2

I have several YouTube videos on one page, I wish that when I play in one, if another video was rolling, pause it. It's possible? Without reloading the page ...

    
asked by anonymous 12.09.2014 / 19:31

1 answer

3

To do this you need to have access to YouTube player events.

function onYouTubePlayerReady(playerId) {
    var ytplayer  = document.getElementById("MyYouTubePlayer");
    ytplayer.addEventListener("onStateChange", "onytplayerStateChange");
}

function onytplayerStateChange(newState) {
    switch (newState) {
        case 0:
            // Encerrado
            break;

        case -1:
            // Não iniciado
            break;

        case 1:
            // Em reprodução
            break;

        case 2:
            // Pausado
            break;

        case 3:
            // Armazenando em buffer
            break;

        case 5:
            // Vídeo iniciado
            break;
    }
}

Take a look at the API

    
12.09.2014 / 21:49