How to play the video section on YouTube?

I am trying to create a youtube video loop in a specific section of a video.

https://www.youtube.com/v/zeI-JD6RO0k?autoplay=1&loop=1&start=30&end=33&playlist=%20zeI-JD6RO0k

From what I know:

To start and end:

start=30&end=33

To make a loop:

autoplay=1&loop=1&playlist=%20zeI-JD6RO0

The problem is that it does not start the next loop while pointing

+4
source share
1 answer

You can use Youtube Iframe-API to encode the video section.

Put this tag on your HTML page:

<!-- 1. The <iframe> (and video player) will replace this <div> tag. -->
<div id="player"></div>

Download Youtube iframe-API

// 2. This code loads the IFrame Player API code asynchronously.
var tag = document.createElement('script');

tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

Create a video with a player and a loop:

var section = {
  start: 30,
  end: 33
};

// 3. This function creates an <iframe> (and YouTube player)
//    after the API code downloads.
var player;
function onYouTubeIframeAPIReady() {
  player = new YT.Player(
    'player',
    {
      height: '360',
      width: '640',
      videoId: 'zeI-JD6RO0k',
      events: {
        'onReady': onPlayerReady,
        'onStateChange': onPlayerStateChange
      }
    }
  );
}

function onPlayerReady(event) {
  player.seekTo(section.start);
  player.playVideo();
}

function onPlayerStateChange(event) {
  if (event.data == YT.PlayerState.PLAYING) {
    var duration = section.end - section.start;
    setTimeout(restartVideoSection, duration * 1000);
  }
}

function restartVideoSection() {
  player.seekTo(section.start);
}

See this example in action.

+5
source

Source: https://habr.com/ru/post/1649458/


All Articles