0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Looping a start-end range of a YouTube video with the IFrame Player API

0
Posted at

When you study a song, a dance move or a lecture, you often want one short range of a YouTube video to play on repeat. The YouTube IFrame Player API can do this in about 40 lines of JavaScript, and it is a nice small exercise in working with player state.

What the embed parameters already give you

The embed URL accepts start and end in whole seconds:

https://www.youtube.com/embed/VIDEO_ID?start=95&end=130

That plays 1:35 to 2:10 once and then stops. There is a loop=1 flag, but it only loops the whole video, and only when you also pass playlist=VIDEO_ID. It ignores your start and end on the second pass. So for a real A-B loop you need the API.

Loading the player

<div id="player"></div>
<script src="https://www.youtube.com/iframe_api"></script>
<script>
  const START = 95, END = 130;
  let player, timer;

  function onYouTubeIframeAPIReady() {
    player = new YT.Player('player', {
      videoId: 'VIDEO_ID',
      playerVars: { start: START, end: END, rel: 0 },
      events: { onStateChange: onState }
    });
  }
</script>

Looping the range

The end player variable fires the ENDED state when the range finishes. Catching that state and seeking back is the simplest loop:

function onState(e) {
  if (e.data === YT.PlayerState.ENDED) {
    player.seekTo(START, true);
    player.playVideo();
  }
}

This works for most videos, but end is only read when the video loads. If the user drags the progress bar past END, the player keeps going. A small watchdog fixes that:

function onState(e) {
  clearInterval(timer);
  if (e.data === YT.PlayerState.PLAYING) {
    timer = setInterval(() => {
      const t = player.getCurrentTime();
      if (t >= END || t < START - 1) player.seekTo(START, true);
    }, 250);
  }
}

Checking every 250 ms keeps the overshoot under a quarter of a second, which is fine for practice. Clear the interval on every state change so you never stack timers.

Things that trip people up

  • getCurrentTime() returns a float, but start and end only take integers. Round START down and END up so you never cut the first or last word.
  • seekTo(seconds, true) with the second argument set to true allows a request to the server for unbuffered parts. Without it, a seek to an unbuffered point can stall.
  • Mobile browsers block autoplay with sound, so the first play must come from a tap.
  • Some videos have embedding turned off by the owner. The player shows an error (code 101 or 150), and no API call can change that.

When you need a file instead of a loop

A loop is great for practice inside the browser. When you need the range as its own short video to share or drop into slides, a trimming tool is quicker than any code. I use Clipavideo for that: paste the link, set the same start and end times, and download the clip.

The API docs list every player method and state code: https://developers.google.com/youtube/iframe_api_reference

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?