Player
An instance of the Player class is created when any of the Video.js setup methods are used to initialize a video.
var myPlayer = videojs('example_video_1');In the following example, the data-setup attribute tells the Video.js library to create a player instance when the library is ready.
After an instance has been created it can be accessed globally using Video('example_video_1').
DEFINED IN: player.js line number: 41
EXTENDS: component.js
Constructor
Player( tag,[options],[ready] )Parameters
| name | Type | Required | Description |
|---|---|---|---|
| tag | Element | yes | The original video tag used for configuring options |
| options | Object | no | Object of option names and values |
| ready | function | no | Ready callback function |
Methods
addRemoteTextTrack( options )
Add a remote text track
Parameters
| name | Type | Required | Description |
|---|---|---|---|
| options | Object | yes | Options for remote text track |
addTextTrack( kind, [label], [language] )
Add a text track In addition to the W3C settings we allow adding additional info through options. http://www.w3.org/html/wg/drafts/html/master/embedded-content-0.html#dom-media-addtexttrack
Parameters
| name | Type | Required | Description |
|---|---|---|---|
| kind | String | yes | Captions, subtitles, chapters, descriptions, or metadata |
| label | String | no | Optional label |
| language | String | no | Optional language |
aspectRatio( [ratio] )
Get/Set the aspect ratio
Parameters
| name | Type | Required | Description |
|---|---|---|---|
| ratio | String | no | Aspect ratio for player |
autoplay( value )
Get or set the autoplay attribute.
Parameters
| name | Type | Required | Description |
|---|---|---|---|
| value | Boolean | yes | Boolean to determine if video should autoplay |
buffered()
Get a TimeRange object with the times of the video that have been downloaded If you just want the percent of the video that's been downloaded, use bufferedPercent.
// Number of different ranges of time have been buffered. Usually 1.
numberOfRanges = bufferedTimeRange.length,
// Time in seconds when the first range starts. Usually 0.
firstRangeStart = bufferedTimeRange.start(0),
// Time in seconds when the first range ends
firstRangeEnd = bufferedTimeRange.end(0),
// Length in seconds of the first time range
firstRangeLength = firstRangeEnd - firstRangeStart;bufferedEnd()
Get the ending time of the last buffered time range This is used in the progress bar to encapsulate all time ranges.
bufferedPercent()
Get the percent (as a decimal) of the video that's been downloaded
var howMuchIsDownloaded = myPlayer.bufferedPercent();0 means none, 1 means all. (This method isn't in the HTML5 spec, but it's very convenient)
canPlayType( type )
Check whether the player can play a given mimetype
Parameters
| name | Type | Required | Description |
|---|---|---|---|
| type | String | yes | The mimetype to check |
controls( bool )
Get or set whether or not the controls are showing.
Parameters
| name | Type | Required | Description |
|---|---|---|---|
| bool | Boolean | yes | Set controls to showing or not |
createEl()
Create the component's DOM element
currentSrc()
Returns the fully qualified URL of the current source value e.g. http://mysite.com/video.mp4
Can be used in conjuction with currentType to assist in rebuilding the current source object.
currentTime( seconds )
Get or set the current time (in seconds)
// get
var whereYouAt = myPlayer.currentTime();
// set
myPlayer.currentTime(120); // 2 minutes into the videoParameters
| name | Type | Required | Description |
|---|---|---|---|
| seconds | Number|String | yes | The time to seek to |
currentType()
Get the current source type e.g. video/mp4 This can allow you rebuild the current source object so that you could load the same source and tech later
dimension( dimension, [value] )
Get/set dimension for player
Parameters
| name | Type | Required | Description |
|---|---|---|---|
| dimension | String | yes | Either width or height |
| value | Number | no | Value for dimension |
dispose()
Destroys the video player and does any necessary cleanup
myPlayer.dispose();This is especially helpful if you are dynamically adding and removing videos to/from the DOM.
duration( seconds )
Get the length in time of the video in seconds
var lengthOfVideo = myPlayer.duration();NOTE: The video must have started loading before the duration can be known, and in the case of Flash, may not be known until the video starts playing.
Parameters
| name | Type | Required | Description |
|---|---|---|---|
| seconds | Number | yes | Duration when setting |
ended()
Returns whether or not the player is in the "ended" state.
enterFullWindow()
When fullscreen isn't supported we can stretch the video container to as wide as the browser will let us.
error( err )
Set or get the current MediaError
Parameters
| name | Type | Required | Description |
|---|---|---|---|
| err | * | yes | A MediaError or a String/Number to be turned into a MediaError |
exitFullscreen()
Return the video to its normal size after having been in full screen mode
myPlayer.exitFullscreen();exitFullWindow()
Exit full window
fluid( bool )
Add/remove the vjs-fluid class
Parameters
| name | Type | Required | Description |
|---|---|---|---|
| bool | Boolean | yes | Value of true adds the class, value of false removes the class |
fullWindowOnEscKey( event )
Check for call to either exit full window or full screen on ESC key
Parameters
| name | Type | Required | Description |
|---|---|---|---|
| event | String | yes | Event to check for key press |
getCache()
Get object for cached values.
static getTagSettings( tag )
Gets tag settings
Parameters
| name | Type | Required | Description |
|---|---|---|---|
| tag | Element | yes | The player tag |
height( [value] )
Get/set player height
Parameters
| name | Type | Required | Description |
|---|---|---|---|
| value | Number | no | Value for height |
init( tag, [options], [ready] )
player's constructor function
Parameters
| name | Type | Required | Description |
|---|---|---|---|
| tag | Element | yes | The original video tag used for configuring options |
| options | Object | no | Player options |
| ready | function | no | Ready callback function |
isFullscreen( [isFS] )
Check if the player is in fullscreen mode
// get
var fullscreenOrNot = myPlayer.isFullscreen();
// set
myPlayer.isFullscreen(true); // tell the player it's in fullscreenNOTE: As of the latest HTML5 spec, isFullscreen is no longer an official property and instead document.fullscreenElement is used. But isFullscreen is still a valuable property for internal player workings.
Parameters
| name | Type | Required | Description |
|---|---|---|---|
| isFS | Boolean | no | Update the player's fullscreen state |
language( code )
The player's language code NOTE: The language should be set in the player options if you want the the controls to be built with a specific language. Changing the lanugage later will not update controls text.
Parameters
| name | Type | Required | Description |
|---|---|---|---|
| code | String | yes | The locale string |
languages()
Get the player's language dictionary Merge every time, because a newly added plugin might call videojs.addLanguage() at any time Languages specified directly in the player options have precedence
load()
Begin loading the src data.
loop( value )
Get or set the loop attribute on the video element.
Parameters
| name | Type | Required | Description |
|---|---|---|---|
| value | Boolean | yes | Boolean to determine if video should loop |
muted( [muted] )
Get the current muted state, or turn mute on or off
// get
var isVolumeMuted = myPlayer.muted();
// set
myPlayer.muted(true); // mute the volumeParameters
| name | Type | Required | Description |
|---|---|---|---|
| muted | Boolean | no | True to mute, false to unmute |
networkState()
Returns the current state of network activity for the element, from the codes in the list below.
- NETWORK_EMPTY (numeric value 0) The element has not yet been initialised. All attributes are in their initial states.
- NETWORK_IDLE (numeric value 1) The element's resource selection algorithm is active and has selected a resource, but it is not actually using the network at this time.
- NETWORK_LOADING (numeric value 2) The user agent is actively trying to download data.
- NETWORK_NO_SOURCE (numeric value 3) The element's resource selection algorithm is active, but it has not yet found a resource to use.
pause()
Pause the video playback
myPlayer.pause();paused()
Check if the player is paused
var isPaused = myPlayer.paused();
var isPlaying = !myPlayer.paused();play()
start media playback
myPlayer.play();playbackRate( rate )
Gets or sets the current playback rate. A playback rate of 1.0 represents normal speed and 0.5 would indicate half-speed playback, for instance.
Parameters
| name | Type | Required | Description |
|---|---|---|---|
| rate | Number | yes | New playback rate to set. |
poster( [src] )
Get or set the poster image source url
EXAMPLE:
// get
var currentPoster = myPlayer.poster();
// set
myPlayer.poster('http://example.com/myImage.jpg');Parameters
| name | Type | Required | Description |
|---|---|---|---|
| src | String | no | Poster image source URL |
preload( value )
Get or set the preload attribute
Parameters
| name | Type | Required | Description |
|---|---|---|---|
| value | Boolean | yes | Boolean to determine if preload should be used |
readyState()
Returns a value that expresses the current state of the element with respect to rendering the current playback position, from the codes in the list below.
- HAVE_NOTHING (numeric value 0) No information regarding the media resource is available.
- HAVE_METADATA (numeric value 1) Enough of the resource has been obtained that the duration of the resource is available.
- HAVE_CURRENT_DATA (numeric value 2) Data for the immediate current playback position is available.
- HAVE_FUTURE_DATA (numeric value 3) Data for the immediate current playback position is available, as well as enough data for the user agent to advance the current playback position in the direction of playback.
- HAVE_ENOUGH_DATA (numeric value 4) The user agent estimates that enough data is available for playback to proceed uninterrupted.
remainingTime()
Calculates how much time is left.
var timeLeft = myPlayer.remainingTime();Not a native video element function, but useful
remoteTextTrackEls()
Get an array of remote html track elements
remoteTextTracks()
Get an array of remote text tracks
removeRemoteTextTrack( track )
Remove a remote text track
Parameters
| name | Type | Required | Description |
|---|---|---|---|
| track | Object | yes | Remote text track to remove |
reportUserActivity( event )
Report user activity
Parameters
| name | Type | Required | Description |
|---|---|---|---|
| event | Object | yes | Event object |
requestFullscreen()
Increase the size of the video to full screen
myPlayer.requestFullscreen();In some browsers, full screen is not supported natively, so it enters "full window mode", where the video fills the browser window. In browsers and devices that support native full screen, sometimes the browser's default controls will be shown, and not the Video.js custom skin. This includes most mobile devices (iOS, Android) and older versions of Safari.
reset()
Reset the player. Loads the first tech in the techOrder,
and calls reset on the tech`.
scrubbing( isScrubbing )
Returns whether or not the user is "scrubbing". Scrubbing is when the user has clicked the progress bar handle and is dragging it along the progress bar.
Parameters
| name | Type | Required | Description |
|---|---|---|---|
| isScrubbing | Boolean | yes | True/false the user is scrubbing |
seekable()
Returns the TimeRanges of the media that are currently available for seeking to.
seeking()
Returns whether or not the player is in the "seeking" state.
selectSource( sources )
Select source based on tech-order or source-order
Uses source-order selection if options.sourceOrder is truthy. Otherwise,
defaults to tech-order selection
Parameters
| name | Type | Required | Description |
|---|---|---|---|
| sources | Array | yes | The sources for a media asset |
src( source )
The source function updates the video source There are three types of variables you can pass as the argument. URL String: A URL to the the video file. Use this method if you are sure the current playback technology (HTML5/Flash) can support the source you provide. Currently only MP4 files can be used in both HTML5 and Flash.
myPlayer.src("http://www.example.com/path/to/video.mp4");*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.
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.
myPlayer.src([
{ type: "video/mp4", src: "http://www.example.com/path/to/video.mp4" },
{ type: "video/webm", src: "http://www.example.com/path/to/video.webm" },
{ type: "video/ogg", src: "http://www.example.com/path/to/video.ogv" }
]);Parameters
| name | Type | Required | Description |
|---|---|---|---|
| source | String|Object|Array | yes | The source URL, object, or array of sources |
supportsFullScreen()
Check to see if fullscreen is supported
tech( )
Return a reference to the current tech.
It will only return a reference to the tech if given an object with the
IWillNotUseThisInPlugins property on it. This is try and prevent misuse
of techs by plugins.
Parameters
| name | Type | Required | Description |
|---|---|---|---|
| undefined | Object |
textTracks()
Get an array of associated text tracks. captions, subtitles, chapters, descriptions http://www.w3.org/html/wg/drafts/html/master/embedded-content-0.html#dom-media-texttracks
toJSON()
Converts track info to JSON
updateStyleEl_()
Update styles of the player element (height, width and aspect ratio)
userActive( bool )
Get/set if user is active
Parameters
| name | Type | Required | Description |
|---|---|---|---|
| bool | Boolean | yes | Value when setting |
videoHeight()
Get video height
videoWidth()
Get video width
volume( percentAsDecimal )
Get or set the current volume of the media
// get
var howLoudIsIt = myPlayer.volume();
// set
myPlayer.volume(0.5); // Set volume to half0 is off (muted), 1.0 is all the way up, 0.5 is half way.
Parameters
| name | Type | Required | Description |
|---|---|---|---|
| percentAsDecimal | Number | yes | The new volume as a decimal percent |
width( [value] )
Get/set player width
Parameters
| name | Type | Required | Description |
|---|---|---|---|
| value | Number | no | Value for width |
$( selector, [context] )
Finds a single DOM element matching selector within the component's
contentEl or another custom context.
Parameters
| name | Type | Required | Description |
|---|---|---|---|
| selector | String | yes | A valid CSS selector, which will be passed to querySelector. |
| context | Element|String | no | A DOM element within which to query. Can also be a selector
string in which case the first matching element will be used
as context. If missing (or no element matches selector), falls
back to document. |
$$( selector, [context] )
Finds a all DOM elements matching selector within the component's
contentEl or another custom context.
Parameters
| name | Type | Required | Description |
|---|---|---|---|
| selector | String | yes | A valid CSS selector, which will be passed to querySelectorAll. |
| context | Element|String | no | A DOM element within which to query. Can also be a selector
string in which case the first matching element will be used
as context. If missing (or no element matches selector), falls
back to document. |
addChild( child, [options] )
Adds a child component inside this component
myComponent.el();
// ->
myComponent.children();
// [empty array]
var myButton = myComponent.addChild('MyButton');
// -> Parameters
name Type Required Description child String|Component yes The class name or instance of a child to add options Object no Options, including options to be passed to children of the child.
addClass( classToAdd )
Add a CSS class name to the component's element
Parameters
name Type Required Description classToAdd String yes Classname to add
buildCSSClass()
Allows sub components to stack CSS class names
children()
Get an array of all child components
var kids = myComponent.children();
clearInterval( intervalId )
Clears an interval and removes the associated dispose listener
Parameters
name Type Required Description intervalId Number yes The id of the interval to clear
clearTimeout( timeoutId )
Clears a timeout and removes the associated dispose listener
Parameters
name Type Required Description timeoutId Number yes The id of the timeout to clear
contentEl()
Return the component's DOM element where children are inserted.
Will either be the same as el() or a new element defined in createEl().
dimensions( width, height )
Set both width and height at the same time
Parameters
name Type Required Description width Number|String yes Width of player height Number|String yes Height of player
el()
Get the component's DOM element
var domEl = myComponent.el();
enableTouchActivity()
Report user touch activity when touch events occur
User activity is used to determine when controls should show/hide. It's
relatively simple when it comes to mouse events, because any mouse event
should show the controls. So we capture mouse events that bubble up to the
player and report activity when that happens.
With touch events it isn't as easy. We can't rely on touch events at the
player level, because a tap (touchstart + touchend) on the video itself on
mobile devices is meant to turn controls off (and on). User activity is
checked asynchronously, so what could happen is a tap event on the video
turns the controls off, then the touchend event bubbles up to the player,
which if it reported user activity, would turn the controls right back on.
(We also don't want to completely block touch events from bubbling up)
Also a touchmove, touch+hold, and anything other than a tap is not supposed
to turn the controls back on on a mobile device.
Here we're setting the default component behavior to report user activity
whenever touch events happen, and this can be turned off by components that
want touch events to act differently.
static extend( props ) (deprecated)
Sets up the constructor using the supplied init method
or uses the init of the parent object
Parameters
name Type Required Description props Object yes An object of properties
getChild()
Returns a child component with the provided name
getChildById()
Returns a child component with the provided ID
static getComponent( name )
Gets a component by name
Parameters
name Type Required Description name String yes Name of the component to get
hasClass( classToCheck )
Check if a component's element has a CSS class name
Parameters
name Type Required Description classToCheck String yes Classname to check
hide()
Hide the component element if currently showing
id()
Get the component's ID
var id = myComponent.id();
initChildren()
Add and initialize default child components from options
// when an instance of MyComponent is created, all children in options
// will be added to the instance by their name strings and options
MyComponent.prototype.options_ = {
children: [
'myChildComponent'
],
myChildComponent: {
myChildOption: true
}
};
// Or when creating the component
var myComp = new MyComponent(player, {
children: [
'myChildComponent'
],
myChildComponent: {
myChildOption: true
}
});
The children option can also be an array of
child options objects (that also include a 'name' key).
This can be used if you have two child components of the
same type that need different options.
var myComp = new MyComponent(player, {
children: [
'button',
{
name: 'button',
someOtherOption: true
},
{
name: 'button',
someOtherOption: false
}
]
});
name()
Get the component's name. The name is often used to reference the component.
var name = myComponent.name();
off( first, second, [third] )
Remove an event listener from this component's element
myComponent.off('eventType', myFunc);
If myFunc is excluded, ALL listeners for the event type will be removed.
If eventType is excluded, ALL listeners will be removed from the component.
Alternatively you can use off to remove listeners that were added to other
elements or components using myComponent.on(otherComponent....
In this case both the event type and listener function are REQUIRED.
myComponent.off(otherElement, 'eventType', myFunc);
myComponent.off(otherComponent, 'eventType', myFunc);
Parameters
name Type Required Description first String|Component yes The event type or other component second function|String yes The listener function or event type third function no The listener for other component
on( first, second, third )
Add an event listener to this component's element
var myFunc = function(){
var myComponent = this;
// Do something when the event is fired
};
myComponent.on('eventType', myFunc);
The context of myFunc will be myComponent unless previously bound.
Alternatively, you can add a listener to another element or component.
myComponent.on(otherElement, 'eventName', myFunc);
myComponent.on(otherComponent, 'eventName', myFunc);
The benefit of using this over VjsEvents.on(otherElement, 'eventName', myFunc)
and otherComponent.on('eventName', myFunc) is that this way the listeners
will be automatically cleaned up when either component is disposed.
It will also bind myComponent as the context of myFunc.
NOTE: When using this on elements in the page other than window
and document (both permanent), if you remove the element from the DOM
you need to call myComponent.trigger(el, 'dispose') on it to clean up
references to it and allow the browser to garbage collect it.
Parameters
name Type Required Description first String|Component yes The event type or other component second function|String yes The event handler or event type third function yes The event handler
one( first, second, [third] )
Add an event listener to be triggered only once and then removed
myComponent.one('eventName', myFunc);
Alternatively you can add a listener to another element or component
that will be triggered only once.
myComponent.one(otherElement, 'eventName', myFunc);
myComponent.one(otherComponent, 'eventName', myFunc);
Parameters
name Type Required Description first String|Component yes The event type or other component second function|String yes The listener function or event type third function no The listener function for other component
options( obj )
Deep merge of options objects
Whenever a property is an object on both options objects
the two properties will be merged using mergeOptions.
Parent.prototype.options_ = {
optionSet: {
'childOne': { 'foo': 'bar', 'asdf': 'fdsa' },
'childTwo': {},
'childThree': {}
}
}
newOptions = {
optionSet: {
'childOne': { 'foo': 'baz', 'abc': '123' }
'childTwo': null,
'childFour': {}
}
}
this.options(newOptions);
RESULT
{
optionSet: {
'childOne': { 'foo': 'baz', 'asdf': 'fdsa', 'abc': '123' },
'childTwo': null, // Disabled. Won't be initialized.
'childThree': {},
'childFour': {}
}
}
Parameters
name Type Required Description obj Object yes Object of new option values
player()
Return the component's player
ready( fn, sync )
Bind a listener to the component's ready state.
Different from event listeners in that if the ready event has already happened
it will trigger the function immediately.
Parameters
name Type Required Description fn function yes Ready listener sync Boolean yes Exec the listener synchronously if component is ready
static registerComponent( name, comp )
Registers a component
Parameters
name Type Required Description name String yes Name of the component to register comp Object yes The component to register
removeChild( component )
Remove a child component from this component's list of children, and the
child component's element from this component's element
Parameters
name Type Required Description component Component yes Component to remove
removeClass( classToRemove )
Remove a CSS class name from the component's element
Parameters
name Type Required Description classToRemove String yes Classname to remove
setInterval( fn, interval )
Creates an interval and sets up disposal automatically.
Parameters
name Type Required Description fn function yes The function to run every N seconds. interval Number yes Number of ms to delay before executing specified function.
setTimeout( fn, timeout )
Creates timeout and sets up disposal automatically.
Parameters
name Type Required Description fn function yes The function to run after the timeout. timeout Number yes Number of ms to delay before executing specified function.
show()
Show the component element if hidden
toggleClass( classToToggle, [predicate] )
Add or remove a CSS class name from the component's element
Parameters
name Type Required Description classToToggle String predicate Boolean|function no Can be a function that returns a Boolean. If true, the class
will be added; if false, the class will be removed. If not
given, the class will be added if not present and vice versa.
trigger( event, [hash] )
Trigger an event on an element
myComponent.trigger('eventName');
myComponent.trigger({'type':'eventName'});
myComponent.trigger('eventName', {data: 'some data'});
myComponent.trigger({'type':'eventName'}, {data: 'some data'});
Parameters
name Type Required Description event Event|Object|String yes A string (the type) or an event object with a type attribute hash Object no data hash to pass along with the event
triggerReady()
Trigger the ready listeners
Events
ended
Fired when video playback ends
error
Fired when an error occurs
loadeddata
Fired when the player has downloaded data at the current playback position
loadedmetadata
Fired when the player has initial duration and dimension information
timeupdate
Fired when the current playback position has changed *
During playback this is fired every 15-250 milliseconds, depending on the
playback technology in use.
useractive
Fired when the user is active, e.g. moves the mouse over the player
userinactive
Fired when the user is inactive, e.g. a short delay after the last mouse move or control interaction
volumechange
Fired when the volume changes