3

Youtube API を使用して、対応するサムネイルによってそれぞれが呼び出される独自の非表示の div に複数のビデオを配置する方法を見つけようとしています。一度非表示になったdivの閉じるボタンを取得して、その特定のビデオを停止する方法を見つけるのに苦労しています。私はそれを正しく行うことができるのは1つだけで、もう1つはそうしません。以下の私のコード:

// 2. This code loads the IFrame Player API code asynchronously.
var tag = document.createElement('script');
tag.src = "//www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

// 3. This function creates an <iframe> (and YouTube player)
//    after the API code downloads.
var player;
function onYouTubeIframeAPIReady() {    
  player1 = new YT.Player('popupVid1', {
    height: '408',
    width: '725',
    videoId: '6Bp1c3-AeXQ',
    playerVars: {
      wmode: "opaque"
    },
    events: {
      'onReady': onPlayerReady,
      'onStateChange': onPlayerStateChange
    }
  });

  player2 = new YT.Player('popupVid2', {
    height: '408',
    width: '725',
    videoId: '6Bp1c3-AeXQ',
    playerVars: {
      wmode: "opaque"
    },
    events: {
      'onReady': onPlayerReady,
      'onStateChange': onPlayerStateChange
    }
  });
}


// 4. The API will call this function when the video player is ready.
function onPlayerReady(event) {
  event.target.playVideo();
}

// 5. The API calls this function when the player's state changes.
//    The function indicates that when playing a video (state=1),
//    the player should play for six seconds and then stop.
var done = false;
function onPlayerStateChange(event) {
  if (event.data == YT.PlayerState.PLAYING && !done) {
    setTimeout(stopVideo,loop);
    done = true;
  }
}

function stopVideo1() {
  player1.stopVideo();
}

function stopVideo2() {
  player2.stopVideo();
}
4

0 に答える 0