In multiple Youtube videos, how to play in one and pause another?

Asked

Viewed 220 times

2

I have several Youtube videos on one page, would like it when I give play in one, if another video was rolling, pause it. Is it possible? Without reloading the page...

1 answer

3

To do this you need 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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.