Warning: HTML preview using PygmentsRenderer failed (TypeError: function() takes at least 2 arguments (0 given))

source: trunk/fl5/doc/publishers/javascriptapi.rst @ 1079

Revision 1079, 13.6 KB checked in by jeroen, 3 years ago (diff)

added docs to flash: intro, embed, http, rtmp, api, architecture. still need to fix references and unify styling.

Line 
1.. _javascriptapi:
2
3Javascript API Reference
4========================
5
6The 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
9Initialization
10--------------
11 
12Please note that the player will **NOT** be available the instant your HML page is loaded and the first javascript is executed. The SWF file (40k) has to be loaded and instantiated first! You can catch this issue by defining a simple global javascript function. It is called *playerReady()* and every player that's succesfully 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
23The *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
37If 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:
38
39.. code-block:: html
40
41   <embed id="myplayer" name="myplayer" src="/upload/player.swf" width="400" height="200" />
42
43You can get a pointer to the player with this line of code:
44
45.. code-block:: html
46
47   var player = document.getElementById('myplayer');
48
49.. note::
50
51   Note you must add both the **id** and **name** attributes in the *<embed>* in order to get back an ID in all browsers.
52
53
54Reading variables
55-----------------
56
57There's two variable calls you can make through the API: *getConfig()* and *getPlaylist()*.
58
59getConfig()
60^^^^^^^^^^^
61
62getConfig() 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:
63
64.. code-block:: html
65
66   var volume = player.getConfig().volume;
67   var width = player.getConfig().width;
68   var state = player.getConfig().state;
69
70Here's the full list of state variables:
71
72.. describe:: bandwidth
73
74   Current bandwidth of the player to the server, in kbps (e.g. *1431*). This is only available for videos, :ref:`http` and :ref:`rtmp`.
75
76.. describe:: fullscreen
77
78   Current fullscreen state of the player, as boolean (e.g. *false*).
79
80.. describe:: height
81
82   Current height of the player, in pixels (e.g. *270*).
83
84.. describe:: item
85
86   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.
87
88.. describe:: level
89
90   Currently active bitrate level, in case multipe bitrates are supplied to the player. This is only useful for  :ref:`http` and :ref:`rtmp`. Note that *0* always refers to the highest quality bitrate.
91
92.. describe:: position
93
94   current playback position, in seconds (e.g. *13.2*).
95
96.. describe:: state
97
98   Current playback state of the player, as an uppercase string. It can be one of the following:
99
100   * *IDLE*: The current playlist item is not loading and not playing.
101   * *BUFFERING*: the current playlistitem is loading. When sufficient data has loaded, it will automatically start playing.
102   * *PLAYING*: the current playlist item is playing.
103   * *PAUSED*: playback of the current playlistitem is not paused by the player.
104   * *COMPLETED*: the current playlist item has completed playback. This state differs from the *IDLE* state in that the item is now already loaded.
105
106.. describe:: mute
107
108   Current audio mute state of the player, as boolean (e.g. *false*).
109
110.. describe:: volume
111
112   Current audio volume of the player, as a number from 0 to 100 (e.g. *90*).
113
114.. describe:: width
115
116   Current width of the player, in pixels (e.g. *480*).
117
118.. Note::
119
120   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.
121
122
123getPlaylist()
124^^^^^^^^^^^^^
125
126getPlaylist() 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 <playlists>` the player recognizes. Here's a few examples:
127
128.. code-block:: html
129
130   var playlist = player.getPlaylist();
131   alert("There are " + playlist.length + " videos in the playlist");
132   alert("The title of the first entry is " + playlist[0].title);
133   alert("The poster image of the second entry is " + playlist[1].image);
134   alert("The media file of the third entry is " + playlist[2].file);
135   alert("The media type of the fourth entry is " + playlist[3].type);
136
137
138Sending events
139--------------
140
141The 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:
142
143.. code-block:: html
144
145   // this will toggle playback.
146   player.sendEvent("play");
147   // this sets the volume to 90%
148   player.sendEvent("volume","true");
149   // This loads a new video in the player
150   player.sendEvent('load','http://content.bitsontherun.com/videos/nPripu9l-60830.mp4');
151
152Here's the full list of events you can send, plus their parameters:
153
154
155.. describe:: item ( index:Number )
156
157   Start playback of a specific item in the playlist. If *index* isn't set, the current playlistitem will start.
158
159.. describe:: link ( index:Number )
160
161   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.
162
163.. describe:: load ( url:String )
164
165   Load a new media file or playlist into the player. The *url* must always be sent.
166
167.. describe:: mute ( state:Boolean )
168
169   Mute or unmute the player's sound. If the *state* is not set, muting will be toggled.
170
171.. describe:: next
172
173   Jump to the next entry in the playlist.  No parameters.
174
175.. describe:: play ( state:Boolean )
176
177   Play (set *state* to *true*) or pause (set *state* to *false*) playback. If the *state* is not set, the player will toggle playback.
178
179.. describe:: prev
180
181   Jump to the previous entry in the playlist.  No parameters.
182
183.. describe:: seek ( position:Number )
184
185   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).
186
187   .. note::
188
189      Seeking does not work if the player is in the *IDLE* state. Make sure to check the *state* variable before attempting to seek.
190
191      Additionally, for the *video* media type, the player can only seek to portions of the video that are already loaded. All other media types (*sound*, *image*, *youtube*, *http* and *rtmp* streaming) do not have this additional restriction.
192
193.. describe:: stop
194
195   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.
196
197.. describe:: volume ( percentage:Number )
198
199   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.
200
201.. note::
202
203   Due to anti-phishing restrictions in the Adobe Flash runtime, it is not possible to enable/disable fullscreen playback of the player from javascript.
204
205Setting listeners
206-----------------
207
208In 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.
209
210Both the *Model* and the *Controller* of the player's :ref:`MVC structure <architecture>` send events. You can subscribe to their events by resp. using the *addModelListener()* and *addControllerListener()* function. Here's a few examples:
211
212.. code-block:: html
213
214   function stateTracker(obj) {
215      alert('the playback state is changed from '+obj.oldstate+' to '+obj.newstate);
216   };
217   player.addModelListener("state","stateTracker");
218
219   function volumeTracker(obj) {
220      alert('the audio volume is changed to: '+obj.percentage'+ percent');
221   };
222   player.addControllerListener("volume","volumeTracker");
223
224If 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:
225
226.. code-block:: html
227
228   player.removeModelListener("state","stateTracker");
229   player.removeControllerListener("volume","volumeTracker");
230
231.. note::
232
233   You MUST string representations of a function for the function parameter!
234
235Model events
236^^^^^^^^^^^^
237
238Here'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() <api>`.
239
240.. describe:: error
241
242   Fired when a playback error occurs (e.g. when the video is not found or the stream is dropped). Data:
243
244   * *message* ( String ): the error message, e.g. *file not found*  or *no suiteable playback codec found*.
245
246.. describe:: loaded
247
248   Fired while the player is busy loading the currently playing media item. This event is never sent for :ref:`rtmp`, since that protocol does not preload content. Data:
249
250   * *loaded* ( Number ): the number of bytes of the media file that are currently loaded.
251   * *total* ( Number ): the total filesize of the media file, in bytes.
252   * *offset* (Number): the byte position of the media file at which loading started. This is always 0, except when using :ref:`http`.
253
254.. describe:: meta
255
256   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:
257
258   * *duration* ( Number) : sent for *video*, *youtube*, *http* and *rtmp* media. In seconds.
259   * *height* ( Number ): sent for all media types, except for *youtube*. In pixels.
260   * *width* ( Number ): sent for all media types, except for *youtube*. In pixels.
261   * Codecs, framerate, seekpoints, channels: sent for *video*, *http* and *rtmp* media.
262   * TimedText, captions, cuepoints: additional metadata that is embedded at a certain position in the media file. Sent for *video*, *http* and *rtmp* media.
263   * ID3 info (genre, name, artist, track, year, comment): sent for MP3 files (the *sound* media type).
264
265
266   .. note::
267
268      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.
269
270.. describe:: state
271
272   Fired when the playback state of the video changes. Data:
273
274   * *oldstate* ( 'IDLE','BUFFERING','PLAYING','PAUSED','COMPLETED' ): the previous playback state.
275   * *newstate* ( 'IDLE','BUFFERING','PLAYING','PAUSED','COMPLETED' ): the new playback state.
276
277.. describe:: time
278
279   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:
280
281   * *duration* ( Number ): total duration of the media file in seconds, e.g. *150* for two and a half minutes.
282   * *position* ( Number ): current playback position in the file, in seconds.
283
284Controller events
285^^^^^^^^^^^^^^^^^
286
287Here'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() <api>`.
288
289.. describe:: item
290
291   Fired when the player switches to a new playlist entry. The new item will immediately start playing. Data:
292
293  * *index* ( Number ): playlist index of the media file that starts playing.
294
295.. describe:: mute
296
297   Fired when the player's audio is muted or unmuted. Data:
298
299   * *state* ( Boolean ): the new mute state. If *true*, the player is muted.
300 
301.. describe:: play
302
303   Fired when the player toggles playback (playing/paused). Data:
304
305   * *state* ( Boolean ): the new playback state. If *true*, the player plays. If *false*, the player pauses.
306
307.. describe:: playlist
308
309   Fired when a new playlist (a single file is also pushed as a playlist!) has been loaded into the player. Data:
310
311   * *playlist* ( Array ): The new playlist. It has exactly the same structure as the return of the *getPlaylist()* call.
312
313.. describe:: resize
314
315   Fired when the player is resized. This includes entering/leaving fullscreen mode. Data:
316
317   * *fullscreen* ( Boolean ): The new fullscreen state. If *true*, the player is in fullscreen.
318   * *height* ( Number ): The overall height of the player.
319   * *width* ( Number ): The overall width of the player.
320
321.. describe:: seek
322
323   Fired when the player is seeking to a new position in the video/sound/image. Parameters:
324
325   * *position* ( Number ): the new position in the file, in seconds (e.g. *150* for two and a half minute).
326
327.. describe:: stop
328
329   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.
330
331.. describe:: volume
332
333   Fired when the volume level is changed. Data:
334
335   * *percentage* ( Number ): new volume percentage, from 0 to 100 (e.g. *90*).
Note: See TracBrowser for help on using the repository browser.