` child element dedicated to video controls. Video controls we will cover later, include: a play/pause button, a fullscreen
button, seek backward and forward buttons, and some elements for current time,
duration and time tracking. `
` Read video metadata First, let's wait for the video metadata to be loaded to set the video
duration, the current time, and initialize the progress bar. Note that the
`secondsToTimeCode()` function is a custom utility function I've written that
converts a number of seconds to a string in "hh:mm:ss" format which is better
suited in our case. `
``video.addEventListener('loadedmetadata', function () { videoDuration.textContent = secondsToTimeCode(video.duration); videoCurrentTime.textContent = secondsToTimeCode(video.currentTime); videoProgressBar.style.transform = `scaleX(${ video.currentTime / video.duration })`; });` **Figure 2.** Media Player showing video metadata
Play/pause video Now that video metadata are loaded, let's add our first button that lets user
play and pause video with `video.play()` and `video.pause()` depending on its
playback state. `
``playPauseButton.addEventListener('click', function (event) { event.stopPropagation(); if (video.paused) { video.play(); } else { video.pause(); } });` Rather than adjusting our video controls in the `click` event listener, we use
the `play` and `pause` video events. Making our controls events based helps
with flexibility (as we'll see later with the Media Session API) and will allow
us to keep our controls in sync if the browser intervenes in the playback.
When video starts playing, we change the
button state to "pause" and hide the video controls. When the video pauses, we
simply change button state to "play" and show the video controls. `video.addEventListener('play', function () { playPauseButton.classList.add('playing'); }); video.addEventListener('pause', function () { playPauseButton.classList.remove('playing'); });` When time indicated by video `currentTime` attribute changed via the
`timeupdate` video event, we also update our custom controls if they're
visible. `video.addEventListener('timeupdate', function () { if (videoControls.classList.contains('visible')) { videoCurrentTime.textContent = secondsToTimeCode(video.currentTime); videoProgressBar.style.transform = `scaleX(${ video.currentTime / video.duration })`; } });` When the video ends, we simply change button state to "play", set video
`currentTime` back to 0 and show video controls for now. Note that we could
also choose to load automatically another video if the user has enabled some
kind of "AutoPlay" feature. `video.addEventListener('ended', function () { playPauseButton.classList.remove('playing'); video.currentTime = 0; });` Seek backward and forward Let's continue and add "seek backward" and "seek forward" buttons so that user
can easily skip some content. `
``var skipTime = 10; // Time to skip in seconds seekForwardButton.addEventListener('click', function (event) { event.stopPropagation(); video.currentTime = Math.min(video.currentTime + skipTime, video.duration); }); seekBackwardButton.addEventListener('click', function (event) { event.stopPropagation(); video.currentTime = Math.max(video.currentTime - skipTime, 0); });` As before, rather than adjusting video styling in the `click` event listeners
of these buttons, we'll use the fired `seeking` and `seeked` video events to
adjust video brightness. My custom `seeking` CSS class is as simple as `filter: brightness(0);`. `video.addEventListener('seeking', function () { video.classList.add('seeking'); }); video.addEventListener('seeked', function () { video.classList.remove('seeking'); });` Here's below what we have created so far. In the next section, we'll implement
the fullscreen button. Fullscreen Here we are going to take advantage of several Web APIs to create a perfect
and seamless fullscreen experience. To see it in action, check out the
[sample](https://googlesamples.github.io/web-fundamentals/fundamentals/media/mobile-web-video-playback.html). Obviously, you don't have to use all of them. Just pick the ones that make
sense to you and combine them to create your custom flow. Prevent automatic fullscreen On iOS, `video` elements automagically enter fullscreen mode when media
playback begins. As we're trying to tailor and control as much as possible our
media experience across mobile browsers, I recommend you set the `playsinline`
attribute of the `video` element to force it to play inline on iPhone and not
enter fullscreen mode when playback begins. Note that this has no side effects
on other browsers. `
playsinline
...
` Toggle fullscreen on button click Now that we prevent automatic fullscreen, we need to handle ourselves the
fullscreen mode for the video with the [Fullscreen API](https://fullscreen.spec.whatwg.org/). When user
clicks the "fullscreen button", let's exit fullscreen mode with
`document.exitFullscreen()` if fullscreen mode is currently in use by the
document. Otherwise, request fullscreen on the video container with the method
`requestFullscreen()` if available or fallback to `webkitEnterFullscreen()` on
the video element only on iOS. `