Brightcove Player API

vjs.Player

EXTENDS: vjs.Component
DEFINED IN: src/js/player.js#L21

An instance of the vjs.Player class is created when any of the Video.js setup methods are used to initialize a video.

<video id="myPlayerID"
  ...>
</video>
...
<script type="text/javascript">
  var myPlayer = videojs('myPlayerID');
</script>

In the following example, a number of the properties tells the Video.js library to create a player instance when the library is ready.

 <video
  data-account="3423279428001"
  data-player="89d39071-6b96-412c-b759-2aff4f7811b3"
  data-embed="default"
  data-video-id="3971877621001"
  class="video-js" controls></video>
The use of any of the following properties in the <video> tag will cause a player instance to be created:
  • data-account
  • data-player
  • data-video-id
  • data-setup

Methods

addChild( child, [options] )

Adds a child component inside this component

myComponent.el();
// -> <div class='my-component'></div>
myComonent.children();
// [empty array]

var myButton = myComponent.addChild('MyButton');
// -> <div class='my-component'><div class="my-button">myButton<div></div>
// -> myButton === myComonent.children()[0];

Pass in options for child constructors and options for children of the child

var myButton = myComponent.addChild('MyButton', {
  text: 'Press Me',
  children: {
    buttonChildExample: {
      buttonChildOption: true
    }
  }
});
Parameters:
  • child String|vjs.Component The class name or instance of a child to add
  • options Object (OPTIONAL) Options, including options to be passed to children of the child.
Returns:
  • vjs.Component The child component (created by this process if a string was used)

inherited from: src/js/component.js#L362


addClass( classToAdd )

Add a CSS class name to the component's element

Parameters:
  • classToAdd String Classname to add
Returns:
  • vjs.Component

inherited from: src/js/component.js#L826


addRemoteTextTrack( trackToAdd )

Add a text track to the player object

Parameters:
  • trackToAdd JSON object Track to add

Example captions track:

var enTrack = myPlayer.addRemoteTextTrack({
 kind: 'captions',
 language: 'en',
 label: 'English',
 src: 'http://solutions.brightcove.com/bcls/captions/adding_captions_to_videos.vtt'
});
Returns:
  • vjs.TextTrackList

autoplay( value )

Get or set the autoplay attribute.

Parameters:
  • value
Returns:
  • String The autoplay attribute value when getting
  • vjs.Player Returns the player when setting

defined in: src/js/player.js#L1275


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;
Returns:
  • Object A mock TimeRange object (following HTML spec)

defined in: src/js/player.js#L782


bufferedEnd()

Get the ending time of the last buffered time range

This is used in the progress bar to encapsulate all time ranges.

Returns:
  • Number The end of the last buffered time range

defined in: src/js/player.js#L833


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)

Returns:
  • Number A decimal between 0 and 1 representing the percent

defined in: src/js/player.js#L802


buildCSSClass()

Allows sub components to stack CSS class names

Returns:
  • String The constructed class name

inherited from: src/js/component.js#L536


cancelFullScreen()

Old naming for exitFullscreen Deprecated true

defined in: src/js/player.js#L1038


catalog.getVideo( videoId, callback)

Makes a catalog request for the video with the specified id and invokes a callback when the request completes

Parameters:
  • videoId String Identifier of a Video Cloud video
  • callback function Callback function for use on video return

For further information see the Player Catalog document.


catalog.load( video )

Loads a video object into the player

Parameters:
  • video Object Video Cloud video object

For further information see the Player Catalog document.


children()

Get an array of all child components

var kids = myComponent.children();
Returns:
  • Array The children

inherited from: src/js/component.js#L296


clearInterval( intervalId )

Clears an interval and removes the associated dispose listener

Parameters:
  • intervalId Number The id of the interval to clear
Returns:
  • Number Returns the interval ID

inherited from: src/js/component.js#L1219


clearTimeout( timeoutId )

Clears a timeout and removes the associated dispose listener

Parameters:
  • timeoutId Number The id of the timeout to clear
Returns:
  • Number Returns the timeout ID

inherited from: src/js/component.js#L1181


contentEl()

Return the component's DOM element for embedding content. Will either be el_ or a new element defined in createEl.

Returns:
  • Element

inherited from: src/js/component.js#L239


controls( controls )

Get or set whether or not the controls are showing.

Parameters:
  • controls Boolean Set controls to showing or not
Returns:
  • Boolean Controls are showing

defined in: src/js/player.js#L1355


createEl( [tagName], [attributes] )

Create the component's DOM element

Parameters:
  • tagName String (OPTIONAL) Element's node type. e.g. 'div'
  • attributes Object (OPTIONAL) An object of element attributes that should be set on the element
Returns:
  • Element

inherited from: src/js/component.js#L200


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

Returns:
  • String The current source

defined in: src/js/player.js#L1242


currentTime( [seconds] )

Get or set the current time (in seconds)

// get
var whereYouAt = myPlayer.currentTime();

// set
myPlayer.currentTime(120); // 2 minutes into the video
Parameters:
  • seconds Number|String (OPTIONAL) The time to seek to
Returns:
  • Number The time in seconds, when not setting
  • vjs.Player self, when the current time is set

defined in: src/js/player.js#L702


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

Returns:
  • String The source MIME type

defined in: src/js/player.js#L1252


dimensions( width, height )

Set both width and height at the same time

Parameters:
  • width Number|String
  • height Number|String
Returns:
  • vjs.Component The component

inherited from: src/js/component.js#L938


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.

defined in: src/js/player.js#L164


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:
  • seconds
Returns:
  • Number The duration of the video in seconds

defined in: src/js/player.js#L730


el()

Get the component's DOM element

var domEl = myComponent.el();
Returns:
  • Element

inherited from: src/js/component.js#L220


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.

inherited from: src/js/component.js#L1120


ended()

Returns whether or not the player is in the "ended" state

Returns:
  • Boolean True if the player is in the ended state, false if not

defined in: src/js/player.js#L1476


error( err )

Set or get the current MediaError

Parameters:
  • err * A MediaError or a String/Number to be turned into a MediaError
Returns:
  • vjs.MediaError|null when getting
  • vjs.Player when setting

defined in: src/js/player.js#L1440


exitFullscreen()

Return the video to its normal size after having been in full screen mode

myPlayer.exitFullscreen();
Returns:
  • vjs.Player self

defined in: src/js/player.js#L1017


getChild( name )

Returns a child component with the provided name

Parameters:
  • name
Returns:
  • vjs.Component

inherited from: src/js/component.js#L330


getChildById( id )

Returns a child component with the provided ID

Parameters:
  • id
Returns:
  • vjs.Component

inherited from: src/js/component.js#L313


hasClass( classToCheck )

Check if a component's element has a CSS class name

Parameters:
  • classToCheck String Classname to check
Returns:
  • vjs.Component

inherited from: src/js/component.js#L816


height( [num], [skipListeners] )

Get or set the height of the component (CSS values)

Setting the video tag dimension values only works with values in pixels. Percent values will not work. Some percents can be used, but width()/height() will return the number + %, not the actual computed width/height.

Parameters:
  • num Number|String (OPTIONAL) New component height
  • skipListeners Boolean (OPTIONAL) Skip the resize event trigger
Returns:
  • vjs.Component This component, when setting the height
  • Number|String The height, when getting

inherited from: src/js/component.js#L927


hide()

Hide the component element if currently showing

Returns:
  • vjs.Component

inherited from: src/js/component.js#L857


id()

Get the component's ID

var id = myComponent.id();
Returns:
  • String

inherited from: src/js/component.js#L258


init( tag, [options], [ready] )

player's constructor function

Parameters:
  • tag Element The original video tag used for configuring options
  • options Object (OPTIONAL) Player options
  • ready Function (OPTIONAL) Ready callback function

defined in: src/js/player.js#L32


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: {
    myChildOption: true
  }
}

// Or when creating the component
var myComp = new MyComponent(player, {
  children: {
    myChildComponent: {
      myChildOption: true
    }
  }
});

The children option can also be an Array of child names or child options objects (that also include a 'name' key).

var myComp = new MyComponent(player, {
  children: [
    'button',
    {
      name: 'button',
      someOtherOption: true
    }
  ]
});

inherited from: src/js/component.js#L481


isFullScreen( isFS )

Old naming for isFullscreen() Deprecated true

Parameters:
  • isFS

defined in: src/js/player.js#L940


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 fullscreen

NOTE: 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:
  • isFS Boolean (OPTIONAL) Update the player's fullscreen state
Returns:
  • Boolean true if fullscreen, false if not
  • vjs.Player self, when setting

defined in: src/js/player.js#L928


language( languageCode )

The player's language code

Parameters:
  • languageCode String The locale string
Returns:
  • String The locale string when getting
  • vjs.Player self, when setting

defined in: src/js/player.js#L124


load()

Begin loading the src data

Returns:
  • vjs.Player Returns the player

defined in: src/js/player.js#L1232


loop( value )

Get or set the loop attribute on the video element

Parameters:
  • value
Returns:
  • String The loop attribute value when getting
  • vjs.Player Returns the player when setting

defined in: src/js/player.js#L1289


muted( [muted] )

Get the current muted state, or turn mute on or off

// get
var isVolumeMuted = myPlayer.muted();

// set
myPlayer.muted(true); // mute the volume
Parameters:
  • muted Boolean (OPTIONAL) True to mute, false to unmute
Returns:
  • Boolean True if mute is on, false if not, when getting
  • vjs.Player self, when setting mute

defined in: src/js/player.js#L890


name()

Get the component's name. The name is often used to reference the component.

var name = myComponent.name();
Returns:
  • String

inherited from: src/js/component.js#L277


off( [first], [second], [third] )

Remove an event listener from the 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:
  • first String|vjs.Component (OPTIONAL) The event type or other component
  • second Function|String (OPTIONAL) The listener function or event type
  • third Function (OPTIONAL) The listener for other component
Returns:
  • vjs.Component

inherited from: src/js/component.js#L646


on( first, second, third )

Add an event listener to this component's element

var myFunc = function(){
  var myPlayer = 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 vjs.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 vjs.trigger(el, "dispose") on it to clean up references to it and allow the browser to garbage collect it.

Parameters:
  • first String|vjs.Component The event type or other component
  • second Function|String The event handler or event type
  • third Function The event handler
Returns:
  • vjs.Component self

inherited from: src/js/component.js#L577


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:
  • first String|vjs.Component The event type or other component
  • second Function|String The listener function or event type
  • third Function (OPTIONAL) The listener function for other component
Returns:
  • vjs.Component

inherited from: src/js/component.js#L691


options( )

Retrieves data from the embed tag and player initialization

Returns:
  • JavaScript object

Example of data returned by the options() method

options console log

Note: If the player was populated with sources only via the catalog, the sources array won't have anything in it


options( obj )

Deep merge of options objects

Whenever a property is an object on both options objects the two properties will be merged using vjs.obj.deepMerge.

This is used for merging options for child components. We want it to be easy to override individual options on a child component without having to rewrite all the other default options.

Parent.prototype.options_ = {
  children: {
    'childOne': { 'foo': 'bar', 'asdf': 'fdsa' },
    'childTwo': {},
    'childThree': {}
  }
}
newOptions = {
  children: {
    'childOne': { 'foo': 'baz', 'abc': '123' }
    'childTwo': null,
    'childFour': {}
  }
}

this.options(newOptions);

RESULT

{
  children: {
    'childOne': { 'foo': 'baz', 'asdf': 'fdsa', 'abc': '123' },
    'childTwo': null, // Disabled. Won't be initialized.
    'childThree': {},
    'childFour': {}
  }
}
Parameters:
  • obj Object Object of new option values
Returns:
  • Object A NEW object of this.options_ and obj merged

inherited from: src/js/component.js#L179


pause()

Pause the video playback

myPlayer.pause();
Returns:
  • vjs.Player self

defined in: src/js/player.js#L671


paused()

Check if the player is paused

var isPaused = myPlayer.paused();
var isPlaying = !myPlayer.paused();
Returns:
  • Boolean false if the media is currently playing, or true otherwise

defined in: src/js/player.js#L684


play()

start media playback

myPlayer.play();
Returns:
  • vjs.Player self

defined in: src/js/player.js#L659


playbackRate( rate )

Gets or sets the current playback rate

Parameters:
  • rate Number New playback rate to set
Returns:
  • Number Returns the new playback rate when setting
  • Number Returns the current playback rate when getting

Caveats

  • Only supported with HTML video, and then only on platforms that have standards-compliant support for different playback speeds
  • Relies on browser capabilities and so will not work everywhere (should work well on iPad, Chrome, and Firefox)

defined in: src/js/player.js#L1618


player()

Return the component's player

Returns:
  • vjs.Player

inherited from: src/js/component.js#L126


poster( [src] )

get or set the poster image source url

EXAMPLE:
// getting
var currentPoster = myPlayer.poster();

// setting
myPlayer.poster('http://example.com/myImage.jpg');
Parameters:
  • src String (OPTIONAL) Poster image source URL
Returns:
  • String poster URL when getting
  • vjs.Player self when setting

defined in: src/js/player.js#L1320


preload( value )

Get or set the preload attribute

Parameters:
  • value
Returns:
  • String The preload attribute value when getting
  • vjs.Player Returns the player when setting

defined in: src/js/player.js#L1261


ready( fn )

Bind a listener to the component's ready state

Different from event listeners in that if the ready event has already happend it will trigger the function immediately.

Parameters:
  • fn Function Ready listener
Returns:
  • vjs.Component

inherited from: src/js/component.js#L769


remainingTime()

Calculates how much time is left

var timeLeft = myPlayer.remainingTime();

Not a native video element function, but useful

Returns:
  • Number The time remaining in seconds

defined in: src/js/player.js#L754


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:
  • component vjs.Component Component to remove

inherited from: src/js/component.js#L420


removeClass( classToRemove )

Remove a CSS class name from the component's element

Parameters:
  • classToRemove String Classname to remove
Returns:
  • vjs.Component

inherited from: src/js/component.js#L837


requestFullScreen()

Old naming for requestFullscreen Deprecated true

defined in: src/js/player.js#L1004


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.

Returns:
  • vjs.Player self

defined in: src/js/player.js#L959


seeking()

Returns whether or not the player is in the "seeking" state

Returns:
  • Boolean True if the player is in the seeking state, false if not

defined in: src/js/player.js#L1482


setInterval( fn, interval )

Creates an interval and sets up disposal automatically

Parameters:
  • fn Function The function to run every N seconds
  • interval Number Number of ms to delay before executing specified function
Returns:
  • Number Returns the interval ID

inherited from: src/js/component.js#L1198


setTimeout( fn, timeout )

Creates timeout and sets up disposal automatically

Parameters:
  • fn Function The function to run after the timeout
  • timeout Number Number of ms to delay before executing specified function
Returns:
  • Number Returns the timeout ID

inherited from: src/js/component.js#L1158


show()

Show the component element if hidden

Returns:
  • vjs.Component

inherited from: src/js/component.js#L847


src( [source], type )

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:
  • source String|Object|Array The source URL, object, or array of sources
  • type String The format of the video; can include
    • video/mp4
    • video/webm
    • video/ogg
    • application/x-mpegURL (for HLS)
Returns:
  • String The current video source when getting
  • String The player when setting

defined in: src/js/player.js#L1147


trigger( event )

Trigger an event on an element

myComponent.trigger('eventName');
myComponent.trigger({'type':'eventName'});
Parameters:
  • event Event|Object|String A string (the type) or an event object with a type attribute
Returns:
  • vjs.Component self

inherited from: src/js/component.js#L724


triggerReady()

Trigger the ready listeners

Returns:
  • vjs.Component

inherited from: src/js/component.js#L788


volume( percentAsDecimal )

Get or set the current volume of the media

// get
var howLoudIsIt = myPlayer.volume();

// set
myPlayer.volume(0.5); // Set volume to half

0 is off (muted), 1.0 is all the way up, 0.5 is half way.

Parameters:
  • percentAsDecimal Number The new volume as a decimal percent
Returns:
  • Number The current volume, when getting
  • vjs.Player self, when setting

defined in: src/js/player.js#L860


width( [num], skipListeners )

Set or get the width of the component (CSS values)

Setting the video tag dimension values only works with values in pixels. Percent values will not work. Some percents can be used, but width()/height() will return the number + %, not the actual computed width/height.

Parameters:
  • num Number|String (OPTIONAL) Optional width number
  • skipListeners Boolean Skip the 'resize' event trigger
Returns:
  • vjs.Component This component, when setting the width
  • Number|String The width, when getting

inherited from: src/js/component.js#L910


Events

durationchange EVENT

Fired when the duration of the media resource is first known or changed

defined in: src/js/player.js#L554


ended EVENT

Fired when the end of the media resource is reached (currentTime == duration)

defined in: src/js/player.js#L541


firstplay EVENT

Fired the first time a video is played

Not part of the HLS spec, and we're not sure if this is the best implementation yet, so use sparingly. If you don't have a reason to prevent playback, use myPlayer.one('play'); instead.

defined in: src/js/player.js#L498


fullscreenchange EVENT

Fired when the player switches in or out of fullscreen mode

defined in: src/js/player.js#L583


loadedalldata EVENT

Fired when the player has finished downloading the source data

defined in: src/js/player.js#L445


loadeddata EVENT

Fired when the player has downloaded data at the current playback position

defined in: src/js/player.js#L439


loadedmetadata EVENT

Fired when the player has initial duration and dimension information

defined in: src/js/player.js#L433


loadstart EVENT

Fired when the user agent begins looking for media data

defined in: src/js/player.js#L389


pause EVENT

Fired whenever the media has been paused

defined in: src/js/player.js#L512


play EVENT

Fired whenever the media begins or resumes playback

defined in: src/js/player.js#L451


progress EVENT

Fired while the user agent is downloading media data

defined in: src/js/player.js#L530


resize EVENT

Fired when the width and/or height of the component changes

inherited from: src/js/component.js#L1020


seeked EVENT

Fired when the player has finished jumping to a new time

defined in: src/js/player.js#L485


seeking EVENT

Fired whenever the player is jumping to a new time

defined in: src/js/player.js#L477


timeupdate EVENT

Fired when the current playback position has changed

During playback this is fired every 15-250 milliseconds, depnding on the playback technology in use.

defined in: src/js/player.js#L524


volumechange EVENT

Fired when the volume changes

defined in: src/js/player.js#L577


waiting EVENT

Fired whenever the media begins waiting

defined in: src/js/player.js#L460