3

我正在尝试找到一种方法来使用 Youtube API 来拥有多个视频,每个视频都在自己的隐藏 div 中,每个视频都由相应的缩略图调用。我很难找出如何让曾经隐藏的 div 的关闭按钮也停止该特定视频。我只能让一个做对,另一个不会。我的代码如下:

// 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