| 1 | .. _javascriptapi: |
|---|
| 2 | |
|---|
| 3 | Javascript API |
|---|
| 4 | ============== |
|---|
| 5 | |
|---|
| 6 | The JW Player for Flash supports a flexible javascript API. It is possible to read the config/playlist variables of the player, send events to the player (e.g. to pause or load a new video) and listen (and respond) to player events. A small initialization routine is needed to connect your apps to the player. |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | Initialization |
|---|
| 10 | -------------- |
|---|
| 11 | |
|---|
| 12 | Please note that the player will **NOT** be available the instant your HTML page is loaded and the first javascript is executed. The SWF file (90k) has to be loaded and instantiated first! You can catch this issue by defining a simple global javascript function. By default, it is called *playerReady()* and every player that's successfully instantiated will call it. |
|---|
| 13 | |
|---|
| 14 | .. code-block:: html |
|---|
| 15 | |
|---|
| 16 | var player; |
|---|
| 17 | function playerReady(*object*) { |
|---|
| 18 | alert('the player is ready'); |
|---|
| 19 | player = document.getElementById(object.id); |
|---|
| 20 | }; |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | The *object* the player send to the function contain the following variables: |
|---|
| 24 | |
|---|
| 25 | .. describe:: id |
|---|
| 26 | |
|---|
| 27 | ID of the player (the *<embed>* code) in the HTML DOM. Use it to get a reference to the player with *getElementById()*. |
|---|
| 28 | |
|---|
| 29 | .. describe:: version |
|---|
| 30 | |
|---|
| 31 | Exact version of the player in MAJOR.MINOR.REVISION format *e.g. 4.7.1017*. |
|---|
| 32 | |
|---|
| 33 | .. describe:: client |
|---|
| 34 | |
|---|
| 35 | Plugin version and platform the player uses, e.g. *FLASH WIN 10.0.47.0*. |
|---|
| 36 | |
|---|
| 37 | .. describe:: id ( undefined ) |
|---|
| 38 | |
|---|
| 39 | Unique identifier of the player in the HTML DOM. You only need to set this option if you want to use the :ref:`javascriptapi` and want to target Linux users. |
|---|
| 40 | |
|---|
| 41 | .. note:: On Windows and MAC, the player automatically reads its *ID* from the *id* and *name* attributes of the player's `HTML embed code <embedding>`. On Linux however, this functionality **does not work**. If you target Linux users with your scripting, you can circumvent this issue by including an ref:`id option <options-behaviour>` in your list of flashvars in the embed code. |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | Custom playerready |
|---|
| 45 | ^^^^^^^^^^^^^^^^^^ |
|---|
| 46 | |
|---|
| 47 | It is possible to ask the player to call a different javascript function after it completed its initialization. This can be done with an :ref:`option <options>` called **playerready**. Here is an example SWFObject :ref:` embed code <embedding>` using the function *registerPlayer()*: |
|---|
| 48 | |
|---|
| 49 | .. code-block:: html |
|---|
| 50 | |
|---|
| 51 | <p id="container1">You don't have Flash ...</p> |
|---|
| 52 | |
|---|
| 53 | <script type="text/javascript"> |
|---|
| 54 | var flashvars = { file:'/data/bbb.mp4',playerready:'registerPlayer' }; |
|---|
| 55 | var params = { allowfullscreen:'true', allowscriptaccess:'always' }; |
|---|
| 56 | var attributes = { id:'player1', name:'player1' }; |
|---|
| 57 | swfobject.embedSWF('player.swf','container1','480','270','9.0.115','false', |
|---|
| 58 | flashvars, params, attributes); |
|---|
| 59 | |
|---|
| 60 | var player; |
|---|
| 61 | function registerPlayer(obj) { |
|---|
| 62 | alert('The player with ID '+obj.id + 'is ready!'); |
|---|
| 63 | player = document.getElementById(obj.id); |
|---|
| 64 | }; |
|---|
| 65 | </script> |
|---|
| 66 | |
|---|
| 67 | No playerready |
|---|
| 68 | ^^^^^^^^^^^^^^ |
|---|
| 69 | |
|---|
| 70 | If you are not interested in calling the player when the page is loading, you won't need the *playerReady()* function. You can then simply use the ID of the embed/object tag that embeds the player to get a reference. So for example with this embed tag: |
|---|
| 71 | |
|---|
| 72 | .. code-block:: html |
|---|
| 73 | |
|---|
| 74 | <embed id="myplayer" name="myplayer" src="/upload/player.swf" width="400" height="200" /> |
|---|
| 75 | |
|---|
| 76 | You can get a pointer to the player with this line of code: |
|---|
| 77 | |
|---|
| 78 | .. code-block:: html |
|---|
| 79 | |
|---|
| 80 | var player = document.getElementById('myplayer'); |
|---|
| 81 | |
|---|
| 82 | .. note:: |
|---|
| 83 | |
|---|
| 84 | Note you must add both the **id** and **name** attributes in the *<embedding>* in order to get back an ID in all browsers. |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | Reading variables |
|---|
| 88 | ----------------- |
|---|
| 89 | |
|---|
| 90 | There's two variable calls you can make through the API: *getConfig()* and *getPlaylist()*. |
|---|
| 91 | |
|---|
| 92 | getConfig() |
|---|
| 93 | ^^^^^^^^^^^ |
|---|
| 94 | |
|---|
| 95 | getConfig() returns an object with state variables of the player. For example, here we request the current audio volume, the current player width and the current playback state: |
|---|
| 96 | |
|---|
| 97 | .. code-block:: html |
|---|
| 98 | |
|---|
| 99 | var volume = player.getConfig().volume; |
|---|
| 100 | var width = player.getConfig().width; |
|---|
| 101 | var state = player.getConfig().state; |
|---|
| 102 | |
|---|
| 103 | Here's the full list of state variables: |
|---|
| 104 | |
|---|
| 105 | .. describe:: bandwidth |
|---|
| 106 | |
|---|
| 107 | Current bandwidth of the player to the server, in kbps (e.g. *1431*). This is only available for the :ref:video <mediaformats>`, :ref:`http <httpstreaming>` and :ref:`rtmp <rtmpstreaming>` providers. |
|---|
| 108 | |
|---|
| 109 | .. describe:: fullscreen |
|---|
| 110 | |
|---|
| 111 | Current fullscreen state of the player, as boolean (e.g. *false*). |
|---|
| 112 | |
|---|
| 113 | .. describe:: height |
|---|
| 114 | |
|---|
| 115 | Current height of the player, in pixels (e.g. *270*). |
|---|
| 116 | |
|---|
| 117 | .. describe:: item |
|---|
| 118 | |
|---|
| 119 | Currently active (playing, paused) playlist item, as zero-index (e.g. *0*). Note that *0* means the first playlistitem is playing and *1* means the second one is playing. |
|---|
| 120 | |
|---|
| 121 | .. describe:: level |
|---|
| 122 | |
|---|
| 123 | Currently active bitrate level, in case multipe bitrates are supplied to the player. This is only useful for :ref:`httpstreaming` and :ref:`rtmpstreaming`. Note that *0* always refers to the highest quality bitrate. |
|---|
| 124 | |
|---|
| 125 | .. describe:: position |
|---|
| 126 | |
|---|
| 127 | current playback position, in seconds (e.g. *13.2*). |
|---|
| 128 | |
|---|
| 129 | .. describe:: state |
|---|
| 130 | |
|---|
| 131 | Current playback state of the player, as an uppercase string. It can be one of the following: |
|---|
| 132 | |
|---|
| 133 | * *IDLE*: The current playlist item is not loading and not playing. |
|---|
| 134 | * *BUFFERING*: the current playlistitem is loading. When sufficient data has loaded, it will automatically start playing. |
|---|
| 135 | * *PLAYING*: the current playlist item is playing. |
|---|
| 136 | * *PAUSED*: playback of the current playlistitem is not paused by the player. |
|---|
| 137 | |
|---|
| 138 | .. describe:: mute |
|---|
| 139 | |
|---|
| 140 | Current audio mute state of the player, as boolean (e.g. *false*). |
|---|
| 141 | |
|---|
| 142 | .. describe:: volume |
|---|
| 143 | |
|---|
| 144 | Current audio volume of the player, as a number from 0 to 100 (e.g. *90*). |
|---|
| 145 | |
|---|
| 146 | .. describe:: width |
|---|
| 147 | |
|---|
| 148 | Current width of the player, in pixels (e.g. *480*). |
|---|
| 149 | |
|---|
| 150 | .. Note:: |
|---|
| 151 | |
|---|
| 152 | In fact, all the :ref:`options` will be available in the response to *getConfig()*. In certain edge cases, this might be useful, e.g. when you want to know if the player did **autostart** or not. |
|---|
| 153 | |
|---|
| 154 | |
|---|
| 155 | getPlaylist() |
|---|
| 156 | ^^^^^^^^^^^^^ |
|---|
| 157 | |
|---|
| 158 | getPlaylist() returns the current playlist of the player as an array. Each entry of this array is in turn again a hashmap with all the :ref:`playlist properties <playlistformats>` the player recognizes. Here's a few examples: |
|---|
| 159 | |
|---|
| 160 | .. code-block:: html |
|---|
| 161 | |
|---|
| 162 | var playlist = player.getPlaylist(); |
|---|
| 163 | alert("There are " + playlist.length + " videos in the playlist"); |
|---|
| 164 | alert("The title of the first entry is " + playlist[0].title); |
|---|
| 165 | alert("The poster image of the second entry is " + playlist[1].image); |
|---|
| 166 | alert("The media file of the third entry is " + playlist[2].file); |
|---|
| 167 | alert("The media provider of the fourth entry is " + playlist[3].provider); |
|---|
| 168 | |
|---|
| 169 | Playlist items can contain properties supported by the provider. Examples of such properties are: |
|---|
| 170 | |
|---|
| 171 | * **http.startparam**, when using the :ref:`HTTP provider <httpstreaming>`. |
|---|
| 172 | * **rtmp.loadbalance**, when using the :ref:`RTMP provider <rtmpstreaming>`. |
|---|
| 173 | |
|---|
| 174 | Playlist items can also contain properties supported by certain plugins. Examples of such properties are: |
|---|
| 175 | |
|---|
| 176 | * **hd.file**, which is used by the HD plugin. |
|---|
| 177 | * **captions.file**, which is used by the Captions plugin. |
|---|
| 178 | |
|---|
| 179 | More information, and the full list of 12 default playlist properties, can be found in :ref:`playlistformats`. |
|---|
| 180 | |
|---|
| 181 | Sending events |
|---|
| 182 | -------------- |
|---|
| 183 | |
|---|
| 184 | The player can be controlled from javascript by sending events (e.g. to pause it or change the volume). Sending events to the player is done through the *sendEvent()* call. Some of the event need a parameter and some don't. Here's a few examples: |
|---|
| 185 | |
|---|
| 186 | .. code-block:: html |
|---|
| 187 | |
|---|
| 188 | // this will toggle playback. |
|---|
| 189 | player.sendEvent("play"); |
|---|
| 190 | // this sets the volume to 90% |
|---|
| 191 | player.sendEvent("volume","true"); |
|---|
| 192 | // This loads a new video in the player |
|---|
| 193 | player.sendEvent('load','http://www.mysite.com/videos/bbb.mp4'); |
|---|
| 194 | |
|---|
| 195 | Here's the full list of events you can send, plus their parameters: |
|---|
| 196 | |
|---|
| 197 | |
|---|
| 198 | .. describe:: item ( index:Number ) |
|---|
| 199 | |
|---|
| 200 | Start playback of a specific item in the playlist. If *index* isn't set, the current playlistitem will start. |
|---|
| 201 | |
|---|
| 202 | .. describe:: link ( index:Number ) |
|---|
| 203 | |
|---|
| 204 | Navigate to the *link* of a specific item in the playlist. If *index* is not set, the player will navigate to the link of the current playlistitem. |
|---|
| 205 | |
|---|
| 206 | .. describe:: load ( url:String ) |
|---|
| 207 | |
|---|
| 208 | Load a new media file or playlist into the player. The *url* must always be sent. |
|---|
| 209 | |
|---|
| 210 | .. describe:: mute ( state:Boolean ) |
|---|
| 211 | |
|---|
| 212 | Mute or unmute the player's sound. If the *state* is not set, muting will be toggled. |
|---|
| 213 | |
|---|
| 214 | .. describe:: next |
|---|
| 215 | |
|---|
| 216 | Jump to the next entry in the playlist. No parameters. |
|---|
| 217 | |
|---|
| 218 | .. describe:: play ( state:Boolean ) |
|---|
| 219 | |
|---|
| 220 | Play (set *state* to *true*) or pause (set *state* to *false*) playback. If the *state* is not set, the player will toggle playback. |
|---|
| 221 | |
|---|
| 222 | .. describe:: prev |
|---|
| 223 | |
|---|
| 224 | Jump to the previous entry in the playlist. No parameters. |
|---|
| 225 | |
|---|
| 226 | .. describe:: seek ( position:Number ) |
|---|
| 227 | |
|---|
| 228 | Seek to a certain position in the currently playing media file. The *position* must be in seconds (e.g. *65* for one minute and five seconds). |
|---|
| 229 | |
|---|
| 230 | .. note:: |
|---|
| 231 | |
|---|
| 232 | Seeking does not work if the player is in the *IDLE* state. Make sure to check the *state* variable before attempting to seek. Additionally, for the *video* media :ref:`provider <mediaformats>`, the player can only seek to portions of the video that are already loaded. Other media providers do not have this additional restriction. |
|---|
| 233 | |
|---|
| 234 | .. describe:: stop |
|---|
| 235 | |
|---|
| 236 | Stop playback of the current playlist entry and unload it. The player will revert to the *IDLE* state and the poster image will be shown. No parameters. |
|---|
| 237 | |
|---|
| 238 | .. describe:: volume ( percentage:Number ) |
|---|
| 239 | |
|---|
| 240 | Change the audio volume of the player to a certain percentage (e.g. *90*). If the player is muted, it will automatically be unmuted when a volume event is sent. |
|---|
| 241 | |
|---|
| 242 | .. note:: |
|---|
| 243 | |
|---|
| 244 | Due to anti-phishing restrictions in the Adobe Flash runtime, it is not possible to enable/disable fullscreen playback of the player from javascript. |
|---|
| 245 | |
|---|
| 246 | Setting listeners |
|---|
| 247 | ----------------- |
|---|
| 248 | |
|---|
| 249 | In order to let javascript respond to player updates, you can assign listener functions to various events the player fires. An example of such event is the *volume* one, when the volume of the player is changed. The player will call the listener function with one parameter, a *key:value* populated object that contains more info about the event. |
|---|
| 250 | |
|---|
| 251 | In the naming of the listener functions, the internal architecture of the JW Player sines through a little. Internally, the player is built using a Mode-View-Controller design pattern: |
|---|
| 252 | |
|---|
| 253 | * The *Model* takes care of the actual media playback. It sends events to the View. |
|---|
| 254 | * The *View* distributes all events from the Model to the plugins and API. It also collects all input from the plugins and API. |
|---|
| 255 | * The *Controller* receives and checks all events from the View. In turn, it sends events to the Model. |
|---|
| 256 | |
|---|
| 257 | Basically, the events from the View are those you send out using the *sendEvent()* API function. With two other API functions, you can listen to events from the Model (playback updates) and Controller (control updates). These API functions are *addModelListener()* and *addControllerListener()*. Here's a few examples: |
|---|
| 258 | |
|---|
| 259 | .. code-block:: html |
|---|
| 260 | |
|---|
| 261 | function stateTracker(obj) { |
|---|
| 262 | alert('the playback state is changed from '+obj.oldstate+' to '+obj.newstate); |
|---|
| 263 | }; |
|---|
| 264 | player.addModelListener("state","stateTracker"); |
|---|
| 265 | |
|---|
| 266 | function volumeTracker(obj) { |
|---|
| 267 | alert('the audio volume is changed to: '+obj.percentage'+ percent'); |
|---|
| 268 | }; |
|---|
| 269 | player.addControllerListener("volume","volumeTracker"); |
|---|
| 270 | |
|---|
| 271 | If you only need to listen to a certain event for a limited amount of time (or just once), use the *removeModelListener()* and removeControllerListener()* functions to unsubscribe your listener function. The syntax is exactly the same: |
|---|
| 272 | |
|---|
| 273 | .. code-block:: html |
|---|
| 274 | |
|---|
| 275 | player.removeModelListener("state","stateTracker"); |
|---|
| 276 | player.removeControllerListener("volume","volumeTracker"); |
|---|
| 277 | |
|---|
| 278 | .. note:: |
|---|
| 279 | |
|---|
| 280 | You MUST string representations of a function for the function parameter! |
|---|
| 281 | |
|---|
| 282 | Model events |
|---|
| 283 | ^^^^^^^^^^^^ |
|---|
| 284 | |
|---|
| 285 | Here's an overview of all events the *Model* sends. Note that the data of every event contains the *id*, *version* and *client* parameters that are also sent on :ref:`playerReady <javascriptapi>`. |
|---|
| 286 | |
|---|
| 287 | .. describe:: error |
|---|
| 288 | |
|---|
| 289 | Fired when a playback error occurs (e.g. when the video is not found or the stream is dropped). Data: |
|---|
| 290 | |
|---|
| 291 | * *message* ( String ): the error message, e.g. *file not found* or *no suiteable playback codec found*. |
|---|
| 292 | |
|---|
| 293 | .. describe:: loaded |
|---|
| 294 | |
|---|
| 295 | Fired while the player is busy loading the currently playing media item. This event is never sent for :ref:`rtmpstreaming`, since that protocol does not preload content. Data: |
|---|
| 296 | |
|---|
| 297 | * *loaded* ( Number ): the number of bytes of the media file that are currently loaded. |
|---|
| 298 | * *total* ( Number ): the total filesize of the media file, in bytes. |
|---|
| 299 | * *offset* (Number): the byte position of the media file at which loading started. This is always 0, except when using :ref:`httpstreaming`. |
|---|
| 300 | |
|---|
| 301 | .. describe:: meta |
|---|
| 302 | |
|---|
| 303 | Fired when metadata on the currently playing media file is received. The exact metadata that is sent with this event varies per individual media file. Here are some examples: |
|---|
| 304 | |
|---|
| 305 | * *duration* ( Number) : sent for *video*, *youtube*, *http* and *rtmp* media. In seconds. |
|---|
| 306 | * *height* ( Number ): sent for all media providers, except for *youtube*. In pixels. |
|---|
| 307 | * *width* ( Number ): sent for all media providers, except for *youtube*. In pixels. |
|---|
| 308 | * Codecs, framerate, seekpoints, channels: sent for *video*, *http* and *rtmp* media. |
|---|
| 309 | * TimedText, captions, cuepoints: additional metadata that is embedded at a certain position in the media file. Sent for *video*, *http* and *rtmp* media. |
|---|
| 310 | * ID3 info (genre, name, artist, track, year, comment): sent for MP3 files (the *sound* :ref:`media provider <mediaformats>`). |
|---|
| 311 | |
|---|
| 312 | |
|---|
| 313 | .. note:: |
|---|
| 314 | |
|---|
| 315 | Due to the :ref:`crossdomain` restrictions of Flash, you cannot load a ID3 data from an MP3 on one domain in a player on another domain. This issue can be circumvented by placing a *crossdomain.xml* file on the server that hosts your MP3s. |
|---|
| 316 | |
|---|
| 317 | .. describe:: state |
|---|
| 318 | |
|---|
| 319 | Fired when the playback state of the video changes. Data: |
|---|
| 320 | |
|---|
| 321 | * *oldstate* ( 'IDLE','BUFFERING','PLAYING','PAUSED','COMPLETED' ): the previous playback state. |
|---|
| 322 | * *newstate* ( 'IDLE','BUFFERING','PLAYING','PAUSED','COMPLETED' ): the new playback state. |
|---|
| 323 | |
|---|
| 324 | .. note:: |
|---|
| 325 | |
|---|
| 326 | You will not be able to check if a video is completed by polling for *getConfig().state*. The player will only be in the COMPLETED state for a very short time, before jumping to IDLE again. Always use *addModelListener('state',...)* if you want to check if a video is completed. |
|---|
| 327 | |
|---|
| 328 | .. describe:: time |
|---|
| 329 | |
|---|
| 330 | Fired when the playback position is changing (i.e. the media file is playing). It is fired with a resolution of 1/10 second, so there'll be a lot of events! Data: |
|---|
| 331 | |
|---|
| 332 | * *duration* ( Number ): total duration of the media file in seconds, e.g. *150* for two and a half minutes. |
|---|
| 333 | * *position* ( Number ): current playback position in the file, in seconds. |
|---|
| 334 | |
|---|
| 335 | Controller events |
|---|
| 336 | ^^^^^^^^^^^^^^^^^ |
|---|
| 337 | |
|---|
| 338 | Here's an overview of all events the *Controller* sends. Note that the data of every event contains the *id*, *version* and *client* parameters that are also sent on :ref:`playerReady <javascriptapi>`. |
|---|
| 339 | |
|---|
| 340 | .. describe:: item |
|---|
| 341 | |
|---|
| 342 | Fired when the player switches to a new playlist entry. The new item will immediately start playing. Data: |
|---|
| 343 | |
|---|
| 344 | * *index* ( Number ): playlist index of the media file that starts playing. |
|---|
| 345 | |
|---|
| 346 | .. describe:: mute |
|---|
| 347 | |
|---|
| 348 | Fired when the player's audio is muted or unmuted. Data: |
|---|
| 349 | |
|---|
| 350 | * *state* ( Boolean ): the new mute state. If *true*, the player is muted. |
|---|
| 351 | |
|---|
| 352 | .. describe:: play |
|---|
| 353 | |
|---|
| 354 | Fired when the player toggles playback (playing/paused). Data: |
|---|
| 355 | |
|---|
| 356 | * *state* ( Boolean ): the new playback state. If *true*, the player plays. If *false*, the player pauses. |
|---|
| 357 | |
|---|
| 358 | .. describe:: playlist |
|---|
| 359 | |
|---|
| 360 | Fired when a new playlist (a single file is also pushed as a playlist!) has been loaded into the player. Data: |
|---|
| 361 | |
|---|
| 362 | * *playlist* ( Array ): The new playlist. It has exactly the same structure as the return of the *getPlaylist()* call. |
|---|
| 363 | |
|---|
| 364 | .. describe:: resize |
|---|
| 365 | |
|---|
| 366 | Fired when the player is resized. This includes entering/leaving fullscreen mode. Data: |
|---|
| 367 | |
|---|
| 368 | * *fullscreen* ( Boolean ): The new fullscreen state. If *true*, the player is in fullscreen. |
|---|
| 369 | * *height* ( Number ): The overall height of the player. |
|---|
| 370 | * *width* ( Number ): The overall width of the player. |
|---|
| 371 | |
|---|
| 372 | .. describe:: seek |
|---|
| 373 | |
|---|
| 374 | Fired when the player is seeking to a new position in the video/sound/image. Parameters: |
|---|
| 375 | |
|---|
| 376 | * *position* ( Number ): the new position in the file, in seconds (e.g. *150* for two and a half minute). |
|---|
| 377 | |
|---|
| 378 | .. describe:: stop |
|---|
| 379 | |
|---|
| 380 | Fired when the player stops loading and playing. The playback state will turn to *IDLE* and the position of a video will be set to 0. No data. |
|---|
| 381 | |
|---|
| 382 | .. describe:: volume |
|---|
| 383 | |
|---|
| 384 | Fired when the volume level is changed. Data: |
|---|
| 385 | |
|---|
| 386 | * *percentage* ( Number ): new volume percentage, from 0 to 100 (e.g. *90*). |
|---|