2016-12-21 01:20:31 +02:00
|
|
|
# Usage examples for the functions on videojs
|
|
|
|
|
|
|
|
## Table of Contents
|
|
|
|
|
|
|
|
* [videojs()](#videojs)
|
|
|
|
* [options](#options)
|
|
|
|
* [getComponent()](#getcomponent)
|
2017-01-23 23:21:53 +02:00
|
|
|
* [registerComponent()](#registercomponent)
|
2016-12-21 01:20:31 +02:00
|
|
|
* [getTech()](#gettech)
|
2017-01-23 23:21:53 +02:00
|
|
|
* [registerTech()](#registertech)
|
2016-12-21 01:20:31 +02:00
|
|
|
* [extend()](#extend)
|
|
|
|
* [mergeOptions()](#mergeoptions)
|
|
|
|
* [bind()](#bind)
|
2017-08-14 23:02:48 +02:00
|
|
|
* [registerPlugin()](#plugin)
|
2017-01-23 23:21:53 +02:00
|
|
|
* [xhr()](#xhr)
|
2016-12-21 01:20:31 +02:00
|
|
|
|
|
|
|
## `videojs()`
|
|
|
|
|
|
|
|
```js
|
|
|
|
var myPlayer = videojs('my_video_id');
|
|
|
|
```
|
|
|
|
|
|
|
|
## `options`
|
|
|
|
|
|
|
|
```js
|
|
|
|
videojs.options.autoplay = true
|
|
|
|
// -> all players will autoplay by default
|
|
|
|
```
|
|
|
|
|
|
|
|
## `getComponent()`
|
|
|
|
|
|
|
|
```js
|
|
|
|
var VjsButton = videojs.getComponent('Button');
|
|
|
|
// Create a new instance of the component
|
|
|
|
var myButton = new VjsButton(myPlayer);
|
|
|
|
```
|
|
|
|
|
|
|
|
## `registerComponent()`
|
|
|
|
|
|
|
|
```js
|
|
|
|
// Get a component to subclass
|
|
|
|
var VjsButton = videojs.getComponent('Button');
|
|
|
|
// Subclass the component (see 'extend' doc for more info)
|
|
|
|
var MySpecialButton = videojs.extend(VjsButton, {});
|
|
|
|
// Register the new component
|
2017-02-06 21:41:57 +02:00
|
|
|
videojs.registerComponent('MySpecialButton', MySpecialButton);
|
2016-12-21 01:20:31 +02:00
|
|
|
// (optionally) add the new component as a default player child
|
2017-02-06 21:41:57 +02:00
|
|
|
myPlayer.addChild('MySpecialButton');
|
2016-12-21 01:20:31 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
## `getTech()`
|
|
|
|
|
|
|
|
```js
|
|
|
|
var Html5 = videojs.getTech('Html5');
|
|
|
|
// Create a new instance of the component
|
|
|
|
var html5 = new Html5(options);
|
|
|
|
```
|
|
|
|
|
|
|
|
## `registerTech()`
|
|
|
|
|
|
|
|
```js
|
|
|
|
// get the Html5 Tech
|
|
|
|
var Html5 = videojs.getTech('Html5');
|
|
|
|
var MyTech = videojs.extend(Html5, {});
|
|
|
|
// Register the new Tech
|
2017-02-06 21:41:57 +02:00
|
|
|
videojs.registerTech('MyTech', MyTech);
|
2016-12-21 01:20:31 +02:00
|
|
|
var player = videojs('myplayer', {
|
|
|
|
techOrder: ['myTech', 'html5']
|
|
|
|
});
|
|
|
|
```
|
|
|
|
|
|
|
|
## `extend()`
|
|
|
|
|
|
|
|
```js
|
|
|
|
// Create a basic javascript 'class'
|
|
|
|
function MyClass(name) {
|
|
|
|
// Set a property at initialization
|
|
|
|
this.myName = name;
|
|
|
|
}
|
|
|
|
// Create an instance method
|
|
|
|
MyClass.prototype.sayMyName = function() {
|
|
|
|
alert(this.myName);
|
|
|
|
};
|
|
|
|
// Subclass the exisitng class and change the name
|
|
|
|
// when initializing
|
|
|
|
var MySubClass = videojs.extend(MyClass, {
|
|
|
|
constructor: function(name) {
|
|
|
|
// Call the super class constructor for the subclass
|
|
|
|
MyClass.call(this, name)
|
|
|
|
}
|
|
|
|
});
|
|
|
|
// Create an instance of the new sub class
|
|
|
|
var myInstance = new MySubClass('John');
|
|
|
|
myInstance.sayMyName(); // -> should alert "John"
|
|
|
|
```
|
|
|
|
|
|
|
|
## `mergeOptions()`
|
|
|
|
|
|
|
|
```js
|
|
|
|
var defaultOptions = {
|
|
|
|
foo: true,
|
|
|
|
bar: {
|
|
|
|
a: true,
|
|
|
|
b: [1,2,3]
|
|
|
|
}
|
|
|
|
};
|
|
|
|
var newOptions = {
|
|
|
|
foo: false,
|
|
|
|
bar: {
|
|
|
|
b: [4,5,6]
|
|
|
|
}
|
|
|
|
};
|
|
|
|
var result = videojs.mergeOptions(defaultOptions, newOptions);
|
|
|
|
// result.foo = false;
|
|
|
|
// result.bar.a = true;
|
|
|
|
// result.bar.b = [4,5,6];
|
|
|
|
```
|
|
|
|
|
|
|
|
## `bind()`
|
|
|
|
|
|
|
|
```js
|
|
|
|
var someClass = function() {};
|
|
|
|
var someObj = new someClass();
|
|
|
|
videojs.bind(someObj, function() {
|
|
|
|
// this will be the context of someObj here
|
|
|
|
});
|
|
|
|
```
|
|
|
|
|
2017-08-14 23:02:48 +02:00
|
|
|
## `registerPlugin()`
|
2016-12-21 01:20:31 +02:00
|
|
|
|
2017-03-21 23:13:06 +02:00
|
|
|
**See the [plugin guide](/docs/guides/plugins.md) in the docs for a more detailed example**
|
2016-12-21 01:20:31 +02:00
|
|
|
|
|
|
|
```js
|
|
|
|
// Make a plugin that alerts when the player plays
|
2017-08-14 23:02:48 +02:00
|
|
|
videojs.registerPlugin('myPlugin', function(myPluginOptions) {
|
2016-12-21 01:20:31 +02:00
|
|
|
myPluginOptions = myPluginOptions || {};
|
|
|
|
|
|
|
|
var player = this;
|
|
|
|
var alertText = myPluginOptions.text || 'Player is playing!'
|
|
|
|
|
|
|
|
player.on('play', function() {
|
|
|
|
alert(alertText);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
// USAGE EXAMPLES
|
|
|
|
// EXAMPLE 1: New player with plugin options, call plugin immediately
|
|
|
|
var player1 = videojs('idOne', {
|
|
|
|
myPlugin: {
|
|
|
|
text: 'Custom text!'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
// Click play
|
|
|
|
// --> Should alert 'Custom text!'
|
|
|
|
// EXAMPLE 3: New player, initialize plugin later
|
|
|
|
var player3 = videojs('idThree');
|
|
|
|
// Click play
|
|
|
|
// --> NO ALERT
|
|
|
|
// Click pause
|
|
|
|
// Initialize plugin using the plugin function on the player instance
|
|
|
|
player3.myPlugin({
|
|
|
|
text: 'Plugin added later!'
|
|
|
|
});
|
|
|
|
// Click play
|
|
|
|
// --> Should alert 'Plugin added later!'
|
|
|
|
```
|
|
|
|
|
|
|
|
## `xhr()`
|
|
|
|
|
|
|
|
```js
|
|
|
|
videojs.xhr({
|
|
|
|
body: someJSONString,
|
|
|
|
uri: "/foo",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json"
|
|
|
|
}
|
|
|
|
}, function (err, resp, body) {
|
|
|
|
// check resp.statusCode
|
|
|
|
});
|
|
|
|
```
|