Jwplayer API Examples
Jwplayer API Examples
This page contains a few cases of player - javascript interaction using the player embedder.
Redirecting on complete
When the video has completed, we redirect to a new page.
<div id="container">This'll be the player</div> <script type="text/javascript"> jwplayer("container").setup({ file:"video.mp4", height:400, width:600, events:{ onComplete: function() { window.location = "http://www.cnn.com"; } }, }); </script>
Using SWFObject
When using SWFObject (or another embed script), listeners cannot be set inline. Therefore, a user has to subscribe to the listeners:
<p id="container">Please install the Flash Plugin</p> <script type="text/javascript" src="swfobject.js"></script> <script type="text/javascript"> var flashvars = { file:'/data/bbb.mp4',autostart:'true' }; var params = { allowfullscreen:'true', allowscriptaccess:'always' }; var attributes = { id:'container', name:'container' }; swfobject.embedSWF('player.swf','container','480','270','9.0.115','false',flashvars, params, attributes); jwplayer('container').onComplete(function() { window.location='http://www.cnn.com'; }); </script>
printing a playlist
We print the playlist in HTML. When an item is clicked, it is loaded in the player and autostarted.
<script type="text/javascript"> jwplayer("container").setup({ file:"video.mp4", height:400, width:600 }); </script> <ul> <li><a href="#" onclick="jwplayer().load('video.mp4')">play video.mp4</a></li> <li><a href="#" onclick="jwplayer().load('bbb.mp4')">play bbb.mp4</a></li> <li><a href="#" onclick="jwplayer().load('ed.mp4')">play ed.mp4</a></li> </ul>
HD switch
Technically, this is similar to the playlist printing. Having a separate example will be useful though. Note that we play the video automatically upon switching:
<script type="text/javascript"> jwplayer("container").setup({ file:"video.mp4", height:400, width:600, events: { onPlaylist: function() { this.play(); } } }); </script>
<ul> <li><a href="#" onclick="jwplayer().load('video_320p.mp4')">Play in 320p quality</a></li> <li><a href="#" onclick="jwplayer().load('video_480p.mp4')">Play in SD quality</a></li> <li><a href="#" onclick="jwplayer().load('video_720p.mp4')">Play in HD quality</a></li> </ul>
Note that this code implies a seek() should start playing a video at the offset if the player is idle. This is ticketed for the 5.2 player.
Mimicking lineair tv
Instead of on-demand, we start the playlist in the player based upon the local time (like oldteevee).
<script type="text/javascript"> jwplayer("container").setup({ playlistfile:"playlist.rss", height:400, width:600 }); var seconds = new Date().getMinutes()*60 + new Date().getSeconds()*60; var offset = 0; var list = jwplayer().getPlaylist(); for(var i=0; i<list.length; i++) { if(offset + list[i].duration > seconds) { jwplayer().setItem(i); jwplayer().seek(offset); break; } else { offset += list[i].duration; } } </script>
Using the addDockButton feature (1.1?), the button for doing this can even be added to the player itself.
Player in a lightbox
Here's a simple example of the player in a lightbox, using the jQuery Tools library:
<a href="#" id="button">Show overlay</a> <div class="overlay" id="container"></div> <script type="text/javascript"> $(function() { $('#button").overlay( { effect: 'apple', onLoad: function() { jwplayer("container").setup({ playlistfile:"playlist.rss", height:400, width:600 }); }, onClose: function() { jwplayer("container").remove(); }); }); </script>