javascript - YouTube API - 在设定的开始和结束时间之间循环播放视频

标签 javascript youtube youtube-api youtube-javascript-api

我已经设法在我需要的时候开始和结束视频,但是有什么方法可以循环播放吗?循环选项似乎没有做太多。

fiddle : https://jsfiddle.net/u7nkz292/

代码:

<div id="ytplayer"></div>



<script>
  // Load the IFrame Player API code asynchronously.
  var tag = document.createElement('script');
  tag.src = "https://www.youtube.com/player_api";
  var firstScriptTag = document.getElementsByTagName('script')[0];
  firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

  // Replace the 'ytplayer' element with an <iframe> and
  // YouTube player after the API code downloads.
  var player;
  function onYouTubePlayerAPIReady() {
    player = new YT.Player('ytplayer', {
      height: '360',
      width: '640',
      videoId: 'M7lc1UVf-VE', 
          playerVars: {
      autoplay: 1,        // Auto-play the video on load
      controls: 0,        // Show pause/play buttons in player
      showinfo: 0,        // Hide the video title
      modestbranding: 1,  // Hide the Youtube Logo
      fs: 1,              // Hide the full screen button
      cc_load_policy: 0, // Hide closed captions
      iv_load_policy: 3,  // Hide the Video Annotations
      start: 36,
      end: 45,
      loop: 1,            // Run the video in a loop
      autohide: 0         // Hide video controls when playing
    },
    });
  }
</script>

最佳答案

您可以实现 onStateChange 回调并使用相同的 startSecondsendSeconds 参数加载视频 loadVideoById :

// Load the IFrame Player API code asynchronously.
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/player_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

var videoId = 'M7lc1UVf-VE';
var startSeconds = 36;
var endSeconds = 45;

// Replace the 'ytplayer' element with an <iframe> and
// YouTube player after the API code downloads.
var player;

var playerConfig = {
  height: '360',
  width: '640',
  videoId: videoId,
  playerVars: {
    autoplay: 1, // Auto-play the video on load
    controls: 0, // Show pause/play buttons in player
    showinfo: 0, // Hide the video title
    modestbranding: 1, // Hide the Youtube Logo
    fs: 1, // Hide the full screen button
    cc_load_policy: 0, // Hide closed captions
    iv_load_policy: 3, // Hide the Video Annotations
    start: startSeconds,
    end: endSeconds,
    autohide: 0, // Hide video controls when playing
  },
  events: {
    'onStateChange': onStateChange
  }
};

function onYouTubePlayerAPIReady() {
  player = new YT.Player('ytplayer', playerConfig);
}

function onStateChange(state) {
  if (state.data === YT.PlayerState.ENDED) {
    player.loadVideoById({
      videoId: videoId,
      startSeconds: startSeconds,
      endSeconds: endSeconds
    });
  }
}

Here是一个 fiddle

关于javascript - YouTube API - 在设定的开始和结束时间之间循环播放视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42271570/

相关文章:

javascript - Onscroll,执行操作

reactjs - React Native 上传到 YouTube

jquery - Bootstrap 模态框和 YouTube : Autoplay and Stop on Close

c++ - 用 C++ 将视频上传到 ScreenCast.com

php - 使用 cclive 和 gcap 下载 YouTube 数据

javascript - 静态定位的div框问题

javascript - 诗农catch xmlhttprequest

ios - 在 Swift 的 AVPlayerViewController 中播放来自 Youtube 的视频

youtube - 检索播放列表中包含的视频 ID - YouTube API v3

javascript - 使用开放层打印 SOS 服务的响应