【问题标题】:YouTube API - Loop video between set start and end timesYouTube API - 在设定的开始和结束时间之间循环播放视频
【发布时间】:2017-07-05 10:10:15
【问题描述】:

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

小提琴: 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>

【问题讨论】:

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


【解决方案1】:

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

// 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 是一个小提琴

【讨论】:

  • 太完美了。谢谢!
  • 如果我坐下自动播放:0 它不会禁用自动播放
【解决方案2】:

迭代上面 Bertrand Martel 的伟大 answer

如果您正在循环播放同一视频,我发现您可以通过调用 player.seekTo(startSeconds) 而不是 player.loadVideoById(...) 来最大限度地减少循环之间的延迟。 Here 是比较两种方法的性能的小提琴。

【讨论】:

    猜你喜欢
    • 2013-10-30
    • 2013-10-31
    • 2021-08-02
    • 1970-01-01
    • 1970-01-01
    • 2013-07-20
    • 1970-01-01
    • 1970-01-01
    • 2016-12-21
    相关资源
    最近更新 更多