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

Add the Player class to instanciate when calling videojs function #3335

Closed
jsamr opened this issue May 21, 2016 · 3 comments
Closed

Add the Player class to instanciate when calling videojs function #3335

jsamr opened this issue May 21, 2016 · 3 comments

Comments

@jsamr
Copy link

jsamr commented May 21, 2016

Description

One can technically inherit from the Player component, but one cannot instantiate it properly, as far as I know.
It would be convenient if the fourth optional argument of the videojs would be a class to instantiate from.
Would be very convenient as an alternative to plugins to change player's behaviour.
I can PR the bellow code if you find it useful.

Snippet

NB : dunno if this snippet works with the Player.extend way of inheriting.

/**
 * Doubles as the main function for users to create a player instance and also
 * the main library object.
 * The `videojs` function can be used to initialize or retrieve a player.
 * ```js
 *     var myPlayer = videojs('my_video_id');
 * ```
 *
 * @param  {String|Element} id      Video element or video element ID
 * @param  {Object=} options        Optional options object for config/settings
 * @param  {Function=} ready        Optional ready callback
 * @param  {Function=} PlayerClass  Optional, the constructor of a class inheriting from Player                  
 * @return {Player}                 A player instance
 * @mixes videojs
 * @method videojs
 */
let videojs = function(id, options, ready, PlayerClass=Player) {
    if( ! PlayerClass.prototype instanceof Player && ! PlayerClass === Player ) {
         throw new TypeError('PlayerClass argument must the constructor of a class inheriting from Player, or the Player class itself.');
    }
    // ... 
    // Element may have a player attr referring to an already created player instance.
    // If not, set up a new player and return the instance.
    return tag['player'] || Player.players[tag.playerId] || new PlayerClass(tag, options, ready);

}
@gkatsev
Copy link
Member

gkatsev commented Jun 22, 2016

If you already have a player, why not just create a new instance of it yourself? The videojs function is basically so that you don't need to create the player manually.

@gkatsev
Copy link
Member

gkatsev commented Jul 25, 2016

There's a new PR for this: #3458

@adam187 @sveinburne can you elaborate on the use-case? Why is it necessary over just having you create the Player manually yourself, since you already have a reference to it?

var player = new MyPlayer(playerEl, {});

@adam187
Copy link
Contributor

adam187 commented Jul 26, 2016

@gkatsev This use case looks different than mine. I will describe my use case in depth in PR.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 26, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants