Quantcast
Channel: Active questions tagged youtube-api - Stack Overflow
Viewing all articles
Browse latest Browse all 3831

The 1st video should turn off after clicking the 2nd video

$
0
0

While the 1st video is playing, click the 2nd video.

It should shut off, it does not.

https://jsfiddle.net/4ct8f6rd/

You can see it working in this code: https://jsfiddle.net/m1e3qt2b/

I am not sure what needs to be adjusted so that it works properly.

const tag = document.createElement('script');tag.src = 'https://www.youtube.com/iframe_api';const firstScriptTag = document.getElementsByTagName('script')[0];firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);let players = [];let videoIds = ['6bDmN0z74M8', '6bDmN0z74M8', '6bDmN0z74M8', '6bDmN0z74M8', '6bDmN0z74M8'];let playerVars = {};function onPlayerReady(event) {  player = event.target;  player.setVolume(100);}function onPlayerStateChange(event) {  if (event.data === YT.PlayerState.PLAYING) {    for (let i = 0; i < players.length; i++) {      if (players[i] !== event.target) players[i].pauseVideo();    }  }}function onYouTubeIframeAPIReady() {  for (let i = 0; i < videoIds.length; i++) {    players[i] = new YT.Player('ytplayer'+ i, {      height: '361',      host: 'https://www.youtube-nocookie.com',      width: '642',      videoId: videoIds[i],      playerVars: {        autoplay: 0,        cc_load_policy: 0,        controls: 1,        disablekb: 1,        fs: 0,        iv_load_policy: 3,      },      events: {'onReady': onPlayerReady,'onStateChange': onPlayerStateChange      }    });  }}

Viewing all articles
Browse latest Browse all 3831

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>