Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic quality selector - Reacts to video.js source changes #49

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

DispatchCommit
Copy link
Contributor

I ran into an issue with videojs-hls-quality-selector on my project where I was loading unknown video file types into the player. Some MP4's, some HLS, some MP3's, and only some of them had multiple quality options.

Currently, this plugin will not work if video.js is initialized with anything but an HLS media. This meant if my player loaded an MP4 file first, and then was given an HLS url, I would have to dispose and recreate the entire video.js instance just to have the quality selector appear. Alternatively I could add the quality levels manually, but why shouldn't this plugin be able to detect and react to changes in player source. Video.js can change tech's on the fly, but this plugin only initializes itself once.

So to improve this, I added 1 new event listener to the plugin that registers when the plugin initializes that listens for when new sources are loaded into video.js. When a new source is loaded, the plugin will then run the check for HLS quality levels. If the tech is HLS, then we will show the quality selector button. However if the tech does not support multiple qualities, such as MP4, then we will hide the quality selector button. Every time the source is changed, we check if the button should be visible or not.

This makes the plugin way more flexible and prevents having to constantly recreate the video.js player just to show or hide the selector. and it does so automatically.

These changes have been applied to a copy of videojs-hls-quality-selector locally in our repo to solve the issue mentioned above.
lbryio/lbry-desktop#5539

Please let me know if you would like any changes or modifications to better suit general use cases.

Fixes issue where quality selector does not appear if video.js is not initialized with an HLS file.
Dynamically shows and hides quality selector based on loaded video
Allows for quality selector to be used in the same player that shows non HLS files
Copy link
Contributor Author

@DispatchCommit DispatchCommit left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for considering this PR, I added a few additional comments in this review in case others find this PR from a google search in the future.

this.bindPlayerEvents();
// Ensure dependencies are met
if (!this.player.qualityLevels) {
console.warn(`Error: Missing videojs-contrib-quality-levels plugin (required by videojs-hls-quality-selector)\nhttps://github.com/videojs/videojs-contrib-quality-levels`);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a reminder warning to the console output in case we are missing a dependency.

this.bindPlayerEvents();

// Listen for video source changes
this.player.on('loadedmetadata', () => this.updatePlugin());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the new event listener added that detects when we've loaded a new video into the player.

/**
* Updates the UI when video.js source changes
*/
updatePlugin() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These checks are ran everytime the source video changes.

this._qualityButton.show();
} else {
// Hide the quality selector
this._qualityButton.hide();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We just toggle .hide() / .show() to add or remove the quality selector on demand when video files change.

*/
setupPlugin() {
// Create the quality selector button.
this.createQualityButton();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We now always create the button, but modify it's visibility according to video source

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant