The purpose of this guide is to discuss the new JS API that the JW Player 5.3 introduces. It is a new, shorthand API for interacting with your website. This is a new implementation and is only application to JW Player Version 5.3 and higher.
The 5.3 player introduced a new, shorthand JavaScript API for interacting with your website. This API abstracts any differences between Flash and HTML5; any code you write will work with both technologies.
In JW Player 5.5, we introduced JavaScript plugins. JS Plugins provide a convenient way to package and distribute code written for this API. Simply add a bit of configuration to your player, and your JavaScript will automatically be loaded into the page. See our guide on Building JavaScript Plugins for more information.
For JW Player Versions 5.2 and below, the player used the 4.x JavaScript API. See Javascript API Reference.
First, you'll need to upload the API library (jwplayer.js) to your web server. We recommend putting it, along with player.swf, in a folder called jwplayer in the root of your site. Once it's on your web server, add this bit of code to your HTML pages, in the <head> of your page:
<head>
<script type='text/javascript' src='/jwplayer/jwplayer.js'></script>
</head>
To get a sense of the possibilities of what you can do with the API, here's a quick example that showcases how to control the player from the page:
<div id='container'>Loading the player ...</div>
<script type='text/javascript'>
jwplayer('container').setup({
flashplayer: '/jwplayer/player.swf',
file: '/uploads/video.mp4',
height: 270,
width: 480
});
</script>
<ul>
<li onclick='jwplayer().play()'>Start the player</li>
<li onclick='alert(jwplayer().getPosition())'>Get current position</li>
</ul>
Of course it's also possible to have the player manipulate the page. Here's a second example, using the event block of the JW Player embedder:
Waiting for video to complete…
<div id='container'>Loading the player ...</div>
<script type='text/javascript'>
jwplayer('container').setup({
flashplayer: '/jwplayer/player.swf',
file: '/uploads/video.mp4',
height: 270,
width: 480,
events: {
onComplete: function() {
document.getElementById('status').innerHTML = 'That's all folks!';
}
}
});
</script>
<p id='status'>Waiting for video to complete…</p>
The following sections give a detailed description of the JW Player API, describing how to:
If you embed the player using SWFObject, rather than the built-in setup() function, you can still use the JavaScript API, although you'll need to wait for Flash to be loaded on the page before interacting with the API. SWFObject 2.2 includes a callback function (in this example, named flashLoaded) which is executed when SWFObject has finished embedding Flash into the page. Make sure you wait until this function is called before making any calls to the API.
Here's a simple example of using the SWFObject callback:
var flashvars = { file:'/videos/video.mp4' };
var params = { allowfullscreen:'true', allowscriptaccess:'always' };
var attributes = { id:'player', name:'player' };
swfobject.embedSWF('/jwplayer/player.swf', 'container', 320, 240, '9.0.115', 'false',
flashvars, params, attributes, flashLoaded);
function flashLoaded(e) {
// e.ref is a reference to the Flash object. We'll pass it to jwplayer() so the API knows where the player is.
// Add event listeners
jwplayer(e.ref).onReady(function() { alert('Player is ready'); });
jwplayer(e.ref).onPlay(function() { alert('Player is playing'); });
// Interact with the player
jwplayer(e.ref).play();
}
If you embed the player directly using an <object> or <embed> tag, simply pass your tag's id to the API when referencing the player:
<embed
id='player'
name='player'
src='/jwplayer/player.swf'
width='320'
height='240'
allowscriptaccess='always'
allowfullscreen='true'
flashvars='file=/videos/video.mp4'
/>
<script type='text/javascript'>
jwplayer('player').onReady(function() { alert('Player is ready'); });
jwplayer('player').onPlay(function() { alert('Player is playing'); });
jwplayer('player').play();
</script>
The first thing you need to do when attempting to interact with a JW Player, is to get a reference to it. The easiest way, probably sufficient for most use cases is this:
// Start the player on this page
jwplayer().play();
Only when you have multiple players on a page, you need to be more specific on which player you want to interact with. In that case, there are three ways to select a player:
With the id of the element you instantiated the player over:
jwplayer('container').play();
With the actual DOM element itself:
var element = document.getElementById('container'); jwplayer(element).play();
With the index in the list of players on the page (in order of loading):
jwplayer(2).play();Note
The selector jwplayer(0) is actually the same as jwplayer().
Here is a list of all the variables that can be retrieved from the player:
Returns the player's entire playlist, as an array of PlaylistItem objects. Here's an example playlist, with three items:
[
{ duration: 32, file: '/uploads/video.mp4', image: '/uploads/video.jpg' },
{ title: 'cool video', file: '/uploads/bbb.mp4' },
{ duration: 542, file: '/uploads/ed.mp4', start: 129 }
]
Returns the playlist item at the specified index. If the index is not specified, the currently playing playlistItem is returned. The item that is returned is an object with key:value properties (e.g. file, duration and title). Example:
{ duration: 32, file: '/uploads/video.mp4', image: '/uploads/video.jpg' }
Returns the player's current playback state. It can have the following values:
Here is a list of all functions that can be called on the player:
Change the player's fullscreen mode. Parameters:
Note: This function will only work in HTML5 mode, due to Flash restrictions on setting full-screen mode.
Change the player's mute state (no sound). Parameters:
Loads a new playlist into the player. The playlist parameter is required and can take a number of forms:
Array: If an array of PlaylistItem objects is passed, load an entire playlist into the player. Example:
[ { duration: 32, file: '/uploads/video.mp4', image: '/uploads/video.jpg' }, { title: 'cool video', file: '/uploads/bbb.mp4' }, { duration: 542, file: '/uploads/ed.mp4', start: 129 } ]
Object: If a PlaylistItem is passed, load it as a single item into the player. Example:
{ duration: 32, file: '/uploads/video.mp4', image: '/uploads/video.jpg' },
String: Can be an XML playlist, or the link to a single media item (e.g. an MP4 video).
Jumps to the playlist item at the specified index. Parameters:
Resizes the player to the specified dimensions. Parameters:
Note: If a controlbar or playlist is displayed next to the video, the actual video is of course smaller than the overall player.
Toggles playback of the player. Parameters:
Toggles playback of the player. Parameters:
Jump to the specified position within the currently playing item. Parameters:
Sets the player's audio volume. Parameters:
Here is a list of all events the player supports. In JavaScript, you can listen to events by assigning a function to it. Your function should take one argument (the event that is fired). Here is a code example, with some JavaScript that listens to changes in the volume:
jwplayer('container').onVolume(
function(event) {
alert('the new volume is: '+event.volume);
}
);
Note that our official embed method contains a shortcut for assigning event listeners, directly in the embed code:
<div id='container'>Loading the player ...</div>
<script type='text/javascript'>
jwplayer('container').setup({
flashplayer: '/jwplayer/player.swf',
file: '/uploads/video.mp4',
height: 270,
width: 480,
events: {
onVolume: function(event) {
alert('the new volume is: '+event.volume);
}
}
});
</script>
And here's the full event list:
Fired when the currently playing item loads additional data into its buffer. Event attributes:
Fired when an error has occurred in the player. Event attributes:
Fired when the player's fullscreen mode changes. Event attributes:
Fired when new metadata has been discovered in the player. Event attributes:
metadata: Object: dictionary object containing the new metadata.
Fired when the player has gone into or out of the mute state. Event attributes:
Fired when a new playlist has been loaded into the player. Event attributes:
Fired when the playlist index changes to a new playlist item. This event occurs before the player begins playing the new playlist item. Event attributes:
Fired when the player's dimensions have changed (the player is resizing or switching fullscreen). Event attributes:
Fired just before the player begins playing. Unlike the onPlay and onBuffer events, the player will not have begun playing or buffering when onBeforePlay is triggered. This event can be used to prevent playback from occurring by calling the stop() function.
Fired when the player enters the PLAYING state. Event attributes:
Fired when the player enters the PAUSED state. Event attributes:
Fired when the player enters the BUFFERING state. Event attributes:
Fired after a seek has been requested either by scrubbing the controlbar or through the API. Event attributes:
Fired when the player enters the IDLE state. Event attributes:
When the player is playing, fired as the playback position gets updated. This happens with a resolution of 0.1 second, so there's a lot of events! Event attributes:
Fired when the player's volume changes. Event attributes:
Note that every API call to a JW Player in turn returns the player instance. This makes it possible to chain API calls (like with jQuery):
jwplayer().setVolume(50).onComplete(function(){ alert('done!'); }).play();
JavaScript plugins can expose their own APIs. These are available by calling the getPlugin function:
Returns a reference to a JavaScript plugin. Each plugin may expose its own public API. Example:
jwplayer().getPlugin("myplugin").myPublicFunction();
You can access the player components' APIs by calling getPlugin() as well. For example, to hide the controlbar, simply call:
jwplayer().getPlugin("controlbar").hide();
The following list describes the public functions for each of the player's components:
Shows the controlbar, if it is currently hidden.
Hides the controlbar, if it is currently showing.
Executes the callback function when the controlbar is shown. callback receives an argument which contains the following properties:
Executes the callback function when the controlbar is hidden. callback receives an argument which contains the following properties:
Shows the dock, if it is currently hidden.
Hides the dock, if it is currently showing.
Executes the callback function when the dock is shown. callback receives an argument which contains the following properties:
Executes the callback function when the dock is hidden. callback receives an argument which contains the following properties:
Adds a button to the dock, or replaces the existing button if one with the same id already exists.
Shows the display icons, if they are currently hidden.
Hides the display icons, if they are currently showing.
Executes the callback function when the display icon is shown. callback receives an argument which contains the following properties:
Executes the callback function when the display icon is hidden. callback receives an argument which contains the following properties: