Adding Custom Video.js Control Bar buttons
Solution 1
The undefined
is coming from the fact that MyButton
is not a vjs.Component
. The documentation around this is a little unclear and it took me a while to understand what is going on.
The documentation states that the first argument is the "class name or instance of a child to add" - it's referring to a JavaScript class rather than a CSS class. The correct way to add a button is as follows:
var myButton = video.controlBar.addChild('button', {
text: "Press me",
// other options
});
myButton.addClass("html-classname");
This will add the following to your controlbar:
<div class="vjs-control html-classname" aria-live="polite" tabindex="0">
<div class='vjs-control-content">
<span class="vjs-control-text">Press me</span>
</div>
</div>
Solution 2
Starting with v5:
var videoJsButtonClass = videojs.getComponent('Button');
var concreteButtonClass = videojs.extend(videoJsButtonClass, {
// The `init()` method will also work for constructor logic here, but it is
// deprecated. If you provide an `init()` method, it will override the
// `constructor()` method!
constructor: function() {
videoJsButtonClass.call(this, videojsInstance);
}, // notice the comma
handleClick: function(){
// Do your stuff
}
});
var concreteButtonInstance = videojsInstance.controlBar.addChild(new concreteButtonClass());
concreteButtonInstance.addClass("vjs-" + name);
PROFIT!
Admin
Updated on July 15, 2022Comments
-
Admin almost 2 years
I've been working on video.js for like a day so I'm a real newbie when it comes to this stuff Today I just wanted to add a button that will switch between two videos. I did it in jQuery quick and easy. But I would rather do it in javascript to better understand video.js as a whole.
Done so far:
1. I have downloaded the most recent version of video.js from github.
2. Player is working great.
3. Read through the guides.
4. Tried their example code.Their Code for button creation:
var myButton = video.controlBar.addChild('MyButton', { text: 'Press Me', children: { buttonChildExample: { buttonChildOption: true } } });
Error from console : Uncaught TypeError: undefined is not a function
So no addChild() is defined which is odd because its in their docs/api.
Does anyone know how to add buttons to their controlbar ? Any help would be appreciated and if you need any more info let me know. Thanks.
UPDATE:
1) I have wrapped the above code in a videojs.ready() as the documentation suggests. But still to no avail.
2) component = new window['videojs'][componentClass](this.player_ || this, options);
in video.dev.js (line 1655) throws the error "uncaught TypeError: undefined is not a function"
3) Evaluating new window['videojs'] in console gave me the error "TypeError: The element or ID Supplied is not valid. (videojs).
Again thanks for your help in adavanced.
-
oleynikd over 8 yearsCould you please describe this a bit more detailed? How to add this button to
controlBar
? How to assignclick
event handler? Thanks. -
deepelement over 8 yearsFYI, you can totally dig into the button DOM and attach to the 'click' event, but this doesn't capture 'invoke' and stuff like that across frameworks. Just use there inline override to keep out of the 'danger zone'
-
And Finally about 8 yearsThanks, very useful - if only VideoJS would do proper documentation! More details in the v5 changelist: github.com/videojs/video.js/wiki/….