DEV Community

Arhan Khan
Arhan Khan

Posted on

Build a Video Player App with JavaScript

A custom video player is a music player with a picture — and one extra lesson: the same controls, the same time handling, and one new trick, fullscreen. If you built a music player before, this will feel familiar. That is the point: the patterns repeat.

The plan

  1. A <video> element with a playlist of videos.
  2. Play, pause, next, previous, and a fullscreen button.
  3. A progress bar and a time display (1:24 / 4:10).
  4. A click on the video itself toggles play and pause.

The HTML

<video id="video" src="video1.mp4"></video>
<div id="controls">
  <button id="play">▶ Play</button>
  <button id="prev">⏮</button>
  <button id="next">⏭</button>
  <span id="time">0:00 / 0:00</span>
  <input id="progress" type="range" value="0" min="0" max="100">
  <button id="full">⛶ Fullscreen</button>
</div>
<ul id="playlist"></ul>
Enter fullscreen mode Exit fullscreen mode

The JavaScript

const videos = [
  { title: 'Intro to JavaScript', file: 'video1.mp4' },
  { title: 'Arrays Explained', file: 'video2.mp4' },
  { title: 'DOM in 10 Minutes', file: 'video3.mp4' }
];
let current = 0;
const video = document.getElementById('video');

function formatTime(seconds) {
  const m = Math.floor(seconds / 60);
  const s = Math.floor(seconds % 60).toString().padStart(2, '0');
  return `${m}:${s}`;
}

function loadVideo(index) {
  current = (index + videos.length) % videos.length;
  video.src = videos[current].file;
  document.querySelectorAll('#playlist li').forEach((li, i) => {
    li.classList.toggle('active', i === current);
  });
}

function togglePlay() {
  if (video.paused) {
    video.play();
    document.getElementById('play').textContent = '⏸ Pause';
  } else {
    video.pause();
    document.getElementById('play').textContent = '▶ Play';
  }
}

document.getElementById('play').addEventListener('click', togglePlay);
video.addEventListener('click', togglePlay);
document.getElementById('next').addEventListener('click', () => {
  loadVideo(current + 1);
  video.play();
});
document.getElementById('prev').addEventListener('click', () => {
  loadVideo(current - 1);
  video.play();
});
document.getElementById('full').addEventListener('click', () => {
  if (document.fullscreenElement) document.exitFullscreen();
  else video.requestFullscreen();
});

video.addEventListener('timeupdate', () => {
  document.getElementById('time').textContent =
    `${formatTime(video.currentTime)} / ${formatTime(video.duration || 0)}`;
  if (video.duration) {
    document.getElementById('progress').value =
      (video.currentTime / video.duration) * 100;
  }
});

document.getElementById('progress').addEventListener('input', (e) => {
  video.currentTime = (e.target.value / 100) * video.duration;
});

video.addEventListener('ended', () => {
  loadVideo(current + 1);
  video.play();
});

// Build the clickable playlist
document.getElementById('playlist').innerHTML = videos.map((v, i) =>
  `<li data-i="${i}">${v.title}</li>`
).join('');
document.getElementById('playlist').addEventListener('click', (e) => {
  if (e.target.dataset.i !== undefined) {
    loadVideo(Number(e.target.dataset.i));
    video.play();
  }
});

loadVideo(0);
Enter fullscreen mode Exit fullscreen mode

Three details beginners miss

  • Video and audio share the same API. play(), pause(), currentTime, duration, timeupdate — everything you learned from a music player transfers directly.
  • Guard against NaN duration. Before the video's metadata loads, video.duration is NaN. The || 0 in the time display and the if (video.duration) check stop your UI from showing NaN:NaN.
  • Fullscreen needs the element, not the page. video.requestFullscreen() puts the video itself fullscreen. Exiting is document.exitFullscreen() — an easy pair to mix up.

What to add next

Playback speed control, volume slider with a mute button, keyboard shortcuts (space to play/pause, arrows to seek), and remembering the last position in localStorage so a viewer can resume where they left off.

Top comments (0)