Supported API Calls
The 1.1 version of the JW WMV Player introduces a small javascript API. Since Silverlight programming itself is also done in javascript, the player is easily accessible through javascript. Note that Lars Nyboe Andersen created a great HTML page that contains a working example of all javascript possibilities. Very easy to copy-paste!
Requesting variables
When starting a player, javascript has already got a reference to it (the ply variable):
var ply = new jeroenwijering.Player(cnt,src,cfg);
Requesting a configuration variable can be done through the reference to the player. A list of all variables can be found here. And here's an example:
alert('the file is: '+ply.getConfig().file);
Sending events
The player can be controlled by sending events. Each function of the controller is named after these events. Here's the overview:
- PLAY
- LINK
- LOAD (file)
- MUTE
- SCRUB (position)
- STOP
- VOLUME (percentage)
And here's an example of the implementation:
ply.sendEvent('VOLUME',50);
Assigning listeners
Finally, you can assign your own functions to listen to specific events of the player. Each function of the view is named after the corresponding event. Here's the complete list, including the arguments that are pinged to the listeners:
- STATE (oldstate,newstate)
- BUFFER (percentage)
- FULLSCREEN
- LOAD (percentage)
- MUTE
- TIME (elapsed,total)
- VOLUME (percentage)
Here's an example of a function that is assigned as a listener:
function myFunc(ost,nst) {
alert('the state went from '+ost+' to '+nst);
};
ply.addListener('STATE',myFunc);
All event values are number, except for the state event. That can have the values Buffering, Closed, Error, Opening, Paused, Playing, Stopped or Completed.
Listeners on Startup
Note that the assigning of listeners during page load will likely fail, because the XAML has to be loaded yet. In order to fix that, add a small timeout to the listeners. Example:
// Instantiation of the player
var cnt = document.getElementById('placeholder');
var src = "wmvplayer.xaml";
var cfg = {height:"250", width:"320",file:"afraid.wmv",autostart:"true"};
var ply = new jeroenwijering.Player(cnt,src,cfg);
addListeners();
// Subscription of the listener
function addListeners() {
if(ply.view) {
ply.addListener('VOLUME',volumeUpdate);
} else {
setTimeout(addListeners,100);
}
};
// The actual listener
function volumeUpdate(pct) {
alert('the volume was changed to: '+pct);
};
