Looking for expert guidance or developers to work on your project? We love working on existing codebases – let’s chat.
<link href="https://vjs.zencdn.net/7.7.6/video-js.css" rel="stylesheet" />
<script src="https://vjs.zencdn.net/7.7.6/video.js"></script> <!-- Our script goes under the videoJS tag. --> <script src="script.js"></script> </body>
%div.player %video#js-video-horsa.js-video.js-video.video-js.vjs-big-play-centered{ width: 1024, height: 720, controls: '', playsinline: true} %video#js-video-hengest.js-video-hidden.js-video.js-video.video-js.vjs-big-play-centered{ width: 1024, height: 720, playsinline: true, controls: '' } %div.content %button#transition-toggle transition to another video
<div class="player"> <video id="js-video-horsa" class="js-video js-video video-js vjs-big-play-centered" width="1024px" height="720px" controls playsinline></video> <video id="js-video-hengest" class="js-video js-video video-js vjs-big-play-centered" width="1024px" height="720px" controls playsinline></video> </div>
.player { // Used to center the players on the page but not essential. display: flex; justify-content: center; // Allows players to be absolute positioned. position: relative; } .video-js { // Causes players to overlap. position: absolute; } // Keeps hidden player at the back. .js-video-hidden { z-index: -1; }
%div.content %button#transition-toggle transition to another video
<div class="content"> <button id="transition-toggle">transition to another video</button> </div>
.content { display: flex; justify-content: center; position: relative; }
// Player options on instantiation. const options = { autoSetup: false, muted: true, preload: 'metadata', autoplay: false, loadingSpinner: true, html5: { nativeControlsForTouch: false, nativeAudioTracks: false, nativeVideoTracks: false, hls: { limitRenditionByPlayerDimensions: false, smoothQualityChange: true, bandwidth: 6194304, overrideNative: true } } } // Instantiating two players. const horsa = videojs('#js-video-horsa', options) const hengest = videojs('#js-video-hengest', options) // HLS Streams, one for each player. const stream_one = "https://bitdash-a.akamaihd.net/content/sintel/hls/playlist.m3u8" const stream_two = "https://bitdash-a.akamaihd.net/content/MI201109210084_1/m3u8s/f08e80da-bf1d-4e3d-8899-f0f6155f6efa.m3u8" // Assigning a stream to each one of them. horsa.src({ src: stream_one, type: 'application/x-mpegURL' }) hengest.src({ src: stream_two, type: 'application/x-mpegURL' })
// Toggle both players visibility to apply a transition, revealing the hidden one and removing the current visible one from view. const applyTransition = () => { // Gets all instances of players. const allPlayers = videojs.getAllPlayers() // For each player: // 1. Reveals if hidden, hides if visible. // 2. Plays if revealed, pauses if hidden. allPlayers.map((player) => { player.el().classList.toggle('js-video-hidden') const isHidden = player.el().classList.contains('js-video-hidden') if (player.paused() && !isHidden) { player.play() } else { player.pause() } }) }
player.el().classList.toggle('js-video-hidden')
const isHidden = player.el().classList.contains('js-video-hidden')
if (player.paused() && !isHidden) { player.play() } else { player.pause() }
// Getting a reference of the transition button. const transitionButton = document.querySelector('#transition-toggle') // Each time the button is clicked the transition is applied. transitionButton.addEventListener('click', () => { applyTransition(); })
<!-- HAML --> %div.player %video#js-video-horsa.js-video.js-video.video-js.vjs-big-play-centered.col-12{ width: 1024, height: 720, controls: '', playsinline: true} %video#js-video-hengest.js-video-hidden.js-video.js-video.video-js.vjs-big-play-centered.col-12{ width: 1024, height: 720, playsinline: true, controls: '' } %div.content %button#transition-toggle transition to another video
<!-- Same markup in HTML --> <div class="player"> <video id="js-video-horsa" class="js-video js-video video-js vjs-big-play-centered" width="1024px" height="720px" controls playsinline></video> <video id="js-video-hengest" class="js-video js-video video-js vjs-big-play-centered" width="1024px" height="720px" controls playsinline></video> </div> <div class="content"> <button id="transition-toggle">transition to another video</button> </div>
/* CSS */ .player { display: flex; justify-content: center; position: relative; } .content { display: flex; justify-content: center; position: relative; } .video-js { position: absolute; } .js-video-hidden { z-index: -1; }
// JavaScript // Player options on instantiation. const options = { autoSetup: false, muted: true, preload: 'metadata', autoplay: false, loadingSpinner: true, html5: { nativeControlsForTouch: false, nativeAudioTracks: false, nativeVideoTracks: false, hls: { limitRenditionByPlayerDimensions: false, smoothQualityChange: true, bandwidth: 6194304, overrideNative: true } } } // HLS Streams, one for each player. const stream_one = "https://bitdash-a.akamaihd.net/content/sintel/hls/playlist.m3u8" const stream_two = "https://bitdash-a.akamaihd.net/content/MI201109210084_1/m3u8s/f08e80da-bf1d-4e3d-8899-f0f6155f6efa.m3u8" // Instantiating two players. const horsa = videojs('#js-video-horsa', options) const hengest = videojs('#js-video-hengest', options) // Assigning a stream to each one of them. horsa.src({ src: stream_one, type: 'application/x-mpegURL' }) hengest.src({ src: stream_two, type: 'application/x-mpegURL' }) // Toggle both players visibility to apply a transition, revealing the hidden one and removing the current visible one from view. const applyTransition = () => { // Gets all instances of players. const allPlayers = videojs.getAllPlayers() // For each player: // 1. Reveals if hidden, hides if visible. // 2. Plays if revealed, pauses if hidden. allPlayers.map((player) => { player.el().classList.toggle('js-video-hidden') const isHidden = player.el().classList.contains('js-video-hidden') if (player.paused() && !isHidden) { player.play() } else { player.pause() } }) } // Getting a reference of the transition button. const transitionButton = document.querySelector('#transition-toggle') // Each time the button is clicked the transition is applied. transitionButton.addEventListener('click', () => { applyTransition(); })
Discover more about Stornaway by seeing the interactive film the CookiesHQ team made here.