JavaScript Interaction

The plug-in can be scripted and controlled via JavaScript in all the supported browsers. JavaScript allows you to register callbacks to receive events occurring inside the plug-in as well as to call functions on the plug-in object to perform operations. In order to be able to do so, a couple of parameters must be added to the simple tags shown in the previous section.

Scripting Considerations

In order to access and script the plug-in object from JavaScript code in your HTML page the <object> and <embed> tags must have an additional id parameter giving them unique names for JavaScript to access the objects.

As per the w3c standard,each tag, for example:

<object id="ie_plugin" … >
    <embed id="np_plugin" … ></embed>
</object>

In this case we have named the object tag ie_plugin and the embed tag np_plugin.

The final step required to be able to script the plug-in is to add some code that will determine which plugin object name to use, depending on the browser that is currently using the plug-in. This is done by parsing the “user agent” string that identifies the browser.

In our case, the following code snippet needs to be added at the top of the JavaScript block before trying to call any function on the plugin object:

<script language="Javascript">   
    var plugin;   
    if(navigator.userAgent.indexOf('MSIE')   != -1)   
    {   
        plugin = document.getElementById('ie_plugin');   
    }   
        else   
    {   
        plugin = document.getElementById('np_plugin');   
    }   
</script>

This block of code assigns the correct document object into the variable plugin by detecting whether the current browser is Internet Explorer or Safari, or something else.

After this block of code, the plugin variable can be safely used to call any function of the DivX Browser Plug-In JavaScript API (see below).

Event Callbacks

It is possible to define local JavaScript functions to be used by the plug-in as callbacks when something of importance occurs. The name of these functions must be passed as embedding parameters using both the <param> sub-tags and the <embed> tag so that the plug-in knows to call them when an event occurs in all supported browsers.

The following callback parameters are defined:

statusCallback

This parameter can be used to pass the name of a local JavaScript function that will be called when an event occurs: The function must have the following signature:

function myStatusCallback (event)

In this case “myStatusCallback” is a placeholder for your function name. The event parameter will be a number. The following values are possible:

  • INIT_DONE = 0
  • OPEN_DONE = 1
  • VIDEO_END = 2
  • SHUT_DONE = 3
  • EMBEDDED_START = 4
  • EMBEDDED_END = 5
  • WINDOWED_START = 6
  • WINDOWED_END = 7
  • FULLSCREEN_START = 8
  • FULLSCREEN_END = 9
  • STATUS_PLAYING = 10
  • STATUS_PAUSED = 11
  • STATUS_FF = 12
  • STATUS_RW = 13
  • STATUS_STOPPED = 14
  • BUFFERING_START = 15
  • BUFFERING_STOP = 16
  • DOWNLOAD_START = 17
  • DOWNLOAD_FAILED = 18
  • DOWNLOAD_DONE = 19

timeCallback

This parameter can be used to pass the name of a local JavaScript function that will be called during playback to inform of the current playback time. The function must have the following signature:

function myTimeCallback (time)

In this case “myTimeCallback” is a placeholder for your function name. The time parameter will be a long integer value giving the current playback time in seconds. This function will be called every second.

bufferCallback

This parameter can be used to pass the name of a local JavaScript function that will be called during buffering to inform of the current buffer status. The function must have the following signature:

function myBufferCallback (current, total)

In this case “myBufferCallback” is a placeholder for your function name. The current and total parameters will be long integers containing the current and total amount of data (in bytes) of this buffering stage.

downloadCallback

This parameter can be used to pass the name of a local JavaScript function that will be called during download to inform of the current status. The function must have the following signature:

function myDownloadCallback (current, total)

In this case “myDownloadCallback” is a placeholder for your function name. The current and total parameters will be long integers containing the current and total amount of data (in bytes) of the downloaded video.

JavaScript API

The DivX Plus Web Player supports the following direct JavaScript calls:

Version:

The following function allows querying the plugin version on the end user machine. The return value will be in the form “X.X.X” where X are single digit numbers.

string GetVersion();

Setup:

Those functions can be used to alternatively set all the embedding parameters and take the same parameter values as the corresponding embedding parameters. They take effect immediately (if applicable) or during the next call to the Open() function.

void SetMode(string mode);
void SetAllowContextMenu(boolean allow);
void SetAutoPlay(boolean play);
void SetVolume(unsigned long volume);
void SetPreviewImage(string imageURL);

Media Management:

This function allows opening a new local video file or relative/full URL.

void Open(string URL);

Playback:

void Play();
void Pause();
void Stop();
void Mute();
void UnMute();
void Seek(string method, unsigned long percent); // seeks into the video, method can be “DOWN”, “UP”, or “DRAG” to simulate mouse interaction. Call “DOWN” and “UP” in sequence to perform a full seek.

Windowing:

Windowing

void ShowPreferences(); // shows the preferences panel
void ShowContextMenu(); // shows the contextual menu
void GoEmbedded(); // go back into browser-embedded mode
void GoWindowed(); // go into windowed playback mode
void GoFullscreen(); // go into fullscreen playback mode

Media Information:

These calls can only return valid data once the “OPEN_DONE” callback event has been sent by the plug-in. Otherwise the return values are undefined.

unsigned long GetTotalTime(); // in seconds
unsigned long GetVideoWidth(); // in pixels
unsigned long GetVideoHeight(); // in pixels
unsigned long GetNumberOfAudioTracks();
unsigned long GetNumberOfSubtitleTracks();
long GetCurrentAudioTrack(); // returns the index of the currently playing audio track (or -1 if no audio track is selected)<
long GetCurrentSubtitleTrack(); // returns the index of the currently playing subtitle track (or -1 if no subtitle track is selected)
string GetAudioTrackName(unsigned long trackIndex); // returns a readable track name for audio track “trackIndex” (which must be between 0 and trackCount-1)
string GetSubtitleTrackName(unsigned long trackIndex); // returns a readable track name for subtitle track “trackIndex” (which must be between 0 and trackCount-1)
string GetAudioTrackLanguage(unsigned long trackIndex); // returns the twochar ISO 859 language code of audio track “trackIndex” (which must be between 0 and trackCount-1)
string GetSubtitleTrackLanguage(unsigned long trackIndex); // returns the twochar ISO 859 language code of subtitle track “trackIndex” (which must be between 0 and trackCount-1)

Media Management:

These calls can be successful once the “OPEN_DONE” callback event has been sent by the plug-in for the current video.

void SetCurrentAudioTrack(long index); // sets the currently playing audio track to the audio track at index “index” (pass -1 if you want no audio track to be selected)
void SetCurrentSubtitleTrack(long index); // sets the currently playing subtitle track to the subtitle track at index “index” (pass -1 if you want no subtitle track to be selected)