将Youtube Javascript API用于多个视频

时间:2012-10-12 15:54:21

标签: api youtube

我正在尝试找到一种方法来使用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();
}

0 个答案:

没有答案