Player Workflows

This document outlines many considerations for using Video.js for advanced player workflows. Be sure to read the setup guide first!

Table of Contents

Accessing a player that has already been created on a page

After an instance has been created it can be accessed globally in two ways:

  1. By calling videojs('example_video_id');
  2. By using it directly via videojs.players.example_video_id;

Removing Players

No matter the term used for it, web applications are becoming common. Not everything is a static, load-once-and-done web page anymore! This means that developers need to be able to manage the full lifecycle of a video player - from creation to destruction. Video.js supports player removal through the dispose() method.

dispose()

This method is available on all Video.js players and components. It is the only supported method of removing a Video.js player from both the DOM and memory. For example, the following code sets up a player and then disposes it when media playback is complete:

  1. var player = videojs('my-player');
  2. player.on('ended', function() {
  3. this.dispose();
  4. });

Calling dispose() will have a few effects:

  1. Trigger a "dispose" event on the player, allowing for any custom cleanup tasks that need to be run by your integration.
  2. Remove all event listeners from the player.
  3. Remove the player’s DOM element(s).

Additionally, these actions are recursively applied to all the player’s child components.

Note: Do not remove players via standard DOM removal methods: this will leave listeners and other objects in memory that you might not be able to clean up!

Checking if a Player is Disposed

At times, it is useful to know whether or not a player reference in your code is stale. The isDisposed() method is available on all components (including players) for this purpose.

Signs of an Undisposed Player

Seeing an error such as:

  1. TypeError: this.el_.vjs_getProperty is not a function

or

  1. TypeError: Cannot read property 'vdata1234567890' of null

Suggests that a player or component was removed from the DOM without using dispose(). It usually means something tried to trigger an event on it or call a method on it.

Showing and Hiding a Player

It is not recommended that you attempt to toggle the visibility or display of a Video.js player. Instead, players should be created and disposed as needed.

This is relevant to use cases such as displaying a player in a modal/overlay. Rather than keeping a hidden Video.js player in a DOM element, it’s recommended that you create the player when the modal opens and dispose it when the modal closes.

This is particularly relevant where memory/resource usage is concerned (e.g. mobile devices).

Depending on the libraries/frameworks in use, an implementation might look something like this:

  1. modal.on('show', function() {
  2. var videoEl = modal.findEl('video');
  3. modal.player = videojs(videoEl);
  4. });
  5. modal.on('hide', function() {
  6. modal.player.dispose();
  7. });

Changing the volume of a player

Volume for a player can be changed through the volume function on a player. The volume function accepts a number from 0-1. Calling it without an argument will return the current volume.

Example

  1. var myPlayer = videojs('some-player-id');
  2. myPlayer.src({type: 'video/mp4', src: 'http://www.example.com/path/to/video.mp4'});
  3. myPlayer.ready(function() {
  4. // get
  5. var howLoudIsIt = myPlayer.volume();
  6. // set
  7. myPlayer.volume(0.5); // Set volume to half
  8. });

Volume can also be muted (without actually changing the volume value) using the muted function. Calling it without an argument will return the current status of muted on the player.

  1. var myPlayer = videojs('some-player-id');
  2. myPlayer.src({type: 'video/mp4', src: 'http://www.example.com/path/to/video.mp4'});
  3. myPlayer.ready(function() {
  4. // get, should be false
  5. console.log(myPlayer.muted());
  6. // set to true
  7. myPlayer.muted(true);
  8. // get should be true
  9. console.log(myPlayer.muted());
  10. });

Making the player fullscreen

To check if the player is currently fullscreen call the isFullscreen function on a player like so.

  1. var myPlayer = videojs('some-player-id');
  2. myPlayer.src({type: 'video/mp4', src: 'http://www.example.com/path/to/video.mp4'});
  3. myPlayer.ready(function() {
  4. // get, should be false
  5. console.log(myPlayer.isFullscreen());
  6. // set, tell the player it's in fullscreen
  7. myPlayer.isFullscreen(true);
  8. // get, should be true
  9. console.log(myPlayer.isFullscreen());
  10. });

To request that the player enter fullscreen call requestFullscreen.

  1. var myPlayer = videojs('some-player-id');
  2. myPlayer.src({type: 'video/mp4', src: 'http://www.example.com/path/to/video.mp4'});
  3. myPlayer.ready(function() {
  4. myPlayer.requestFullscreen();
  5. });

To exit fullscreen call exitFullscreen

  1. var myPlayer = videojs('some-player-id');
  2. myPlayer.src({type: 'video/mp4', src: 'http://www.example.com/path/to/video.mp4'});
  3. myPlayer.ready(function() {
  4. myPlayer.requestFullscreen();
  5. myPlayer.exitFullscreen();
  6. });

Using Playback information functions

play can be used to start playback on a player that has a source.

  1. var myPlayer = videojs('some-player-id');
  2. myPlayer.src({type: 'video/mp4', src: 'http://www.example.com/path/to/video.mp4'});
  3. myPlayer.ready(function() {
  4. myPlayer.play();
  5. });

pause can be used to pause playback on a player that is playing.

  1. var myPlayer = videojs('some-player-id');
  2. myPlayer.src({type: 'video/mp4', src: 'http://www.example.com/path/to/video.mp4'});
  3. myPlayer.ready(function() {
  4. myPlayer.play();
  5. myPlayer.pause();
  6. });

paused can be used to determine if a player is currently paused.

  1. var myPlayer = videojs('some-player-id');
  2. myPlayer.src({type: 'video/mp4', src: 'http://www.example.com/path/to/video.mp4'});
  3. myPlayer.ready(function() {
  4. // true
  5. console.log(myPlayer.paused());
  6. // false
  7. console.log(!myPlayer.paused());
  8. myPlayer.play();
  9. // false
  10. console.log(myPlayer.paused());
  11. // true
  12. console.log(!myPlayer.paused());
  13. myPlayer.pause();
  14. // true
  15. console.log(myPlayer.paused());
  16. // false
  17. console.log(!myPlayer.paused());
  18. });

currentTime will give you the currentTime (in seconds) that playback is currently occuring at.

  1. var myPlayer = videojs('some-player-id');
  2. myPlayer.src({type: 'video/mp4', src: 'http://www.example.com/path/to/video.mp4'});
  3. myPlayer.ready(function() {
  4. // set current time to 2 minutes into the video
  5. myPlayer.currentTime(120);
  6. // get the current time, should be 120 seconds
  7. var whereYouAt = myPlayer.currentTime();
  8. });

duration will give you the total duration of the video that is playing

  1. var myPlayer = videojs('some-player-id');
  2. myPlayer.src({type: 'video/mp4', src: 'http://www.example.com/path/to/video.mp4'});
  3. myPlayer.ready(function() {
  4. var lengthOfVideo = myPlayer.duration();
  5. });

remainingTime will give you the seconds that are remaing in the video.

  1. var myPlayer = videojs('some-player-id');
  2. myPlayer.src({type: 'video/mp4', src: 'http://www.example.com/path/to/video.mp4'});
  3. myPlayer.ready(function() {
  4. myPlayer.currentTime(10);
  5. // should be 10 seconds less than duration
  6. console.log(myPlayer.remainingTime());
  7. });

buffered will give you a timeRange object representing the current ranges of time that are ready to be played at a future time.

  1. var myPlayer = videojs('some-player-id');
  2. myPlayer.src({type: 'video/mp4', src: 'http://www.example.com/path/to/video.mp4'});
  3. myPlayer.ready(function() {
  4. var bufferedTimeRange = myPlayer.buffered();
  5. // number of different ranges of time have been buffered.
  6. // Usually 1
  7. var numberOfRanges = bufferedTimeRange.length,
  8. // Time in seconds when the first range starts.
  9. // Usually 0
  10. var firstRangeStart = bufferedTimeRange.start(0),
  11. // Time in seconds when the first range ends
  12. var firstRangeEnd = bufferedTimeRange.end(0),
  13. // Length in seconds of the first time range
  14. var firstRangeLength = firstRangeEnd - firstRangeStart;
  15. });

bufferedPercent will give you the the current percentage of the video that is buffered.

  1. var myPlayer = videojs('some-player-id');
  2. myPlayer.src({type: 'video/mp4', src: 'http://www.example.com/path/to/video.mp4'});
  3. myPlayer.ready(function() {
  4. // example 0.11 aka 11%
  5. var howMuchIsDownloaded = myPlayer.bufferedPercent();
  6. });

Dealing with the source or the poster on the player

Passing a source to the player via the API. (this can also be done using options)

  1. var myPlayer = videojs('some-player-id');
  2. myPlayer.src('http://www.example.com/path/to/video.mp4');

When a string is provided as the source, Video.js will try to infer the video type from the file extension, but this inference will not work in all cases. It is recommended that the source is provided as an object including the type, as below.

Source Object (or element): A javascript object containing information about the source file. Use this method if you want the player to determine if it can support the file using the type information.

  1. var myPlayer = videojs('some-player-id');
  2. myPlayer.src({type: 'video/mp4', src: 'http://www.example.com/path/to/video.mp4'});

Array of Source Objects: To provide multiple versions of the source so that it can be played using HTML5 across browsers you can use an array of source objects. Video.js will detect which version is supported and load that file.

  1. var myPlayer = videojs('some-player-id');
  2. myPlayer.src([
  3. {type: 'video/mp4', src: 'http://www.example.com/path/to/video.mp4'},
  4. {type: 'video/webm', src: 'http://www.example.com/path/to/video.webm'},
  5. {type: 'video/ogg', src: 'http://www.example.com/path/to/video.ogv'}
  6. ]);

Changing or setting the poster via the API. (this can also be done with options)

  1. var myPlayer = videojs('example_video_1');
  2. // set
  3. myPlayer.poster('http://example.com/myImage.jpg');
  4. // get
  5. console.log(myPlayer.poster());
  6. // 'http://example.com/myImage.jpg'

Accessing the Tech on the player

The tech on the player can be accessed via tech(). Passing any argument will silence the warning that is logged.

  1. var myPlayer = videojs('some-player-id');
  2. myPlayer.src({type: 'video/mp4', src: 'http://www.example.com/path/to/video.mp4'});
  3. myPlayer.ready(function() {
  4. // tech() will log warning without any argument
  5. var tech = myPlayer.tech(false);
  6. });

Using Video.js with…

jQuery

React

See ReactJS integration example

Ember

Angular

Vue

See Vue integration example