From: [EMAIL PROTECTED]
Subject: Javascript Bridge for RealPlayer and Applet

This is a follow-up to my earlier post about solving some mysterious browser
crashes while running RealPlayer. Several people have asked for my JavaScript
code, so I thought I would go ahead and post it here for everyone else.

Unfortunately the applet I wrote is considered proprietary yadda yadda yadda.
Enclosed you will find a JavaScript routine called Monitor.js that is 
loaded via
the following snippet placed within a <HEAD> block:

     <SCRIPT LANGUAGE="JavaScript">
     var gRealPlayerName = "ImageViewer";
     var gHitIndicatorName = "HitIndicator";
     </SCRIPT>
     <SCRIPT LANGUAGE="JavaScript" FILE="Monitor.js"></SCRIPT>

The Monitor.js file has a routine called Monitor() that communicates 
between the
RealPlayer plug-in and my applet. Using a timer, it periodically polls both the
RealPlayer and the applet and forwards changes in one to the other. It uses two
JavaScript global variables, gRealPlayerName and gHitIndicatorName, as indexes
into certain document arrays to find the appropriate objects. Thus, these 
values
should match the NAME parameter given in the <EMBED> and <APPLET> tags in the
HTML body.

Finally, you need to install the Monitor.js routines by using the onLoad and
onUnload routines in the <BODY> tag like so:

     <BODY onLoad="Install();" onUnload="Uninstall();">

That's it. So far, I have not had any failures in Netscape or IE5 even after
repeated reloads.

Brad


 >%--->%--->%--->%--->%--->%--->%--->%--->%--->%--->%--->%--->%---

// -*- Mode: Java -*-
//
// Monitor -- JavaScript routines that attempt to communicate between a
// RealPlayer plug-in and an applet that shows audio hits from a search. The
// basic approach is there are two timers installed when the page loads. One
// watches for changes in the RealPlayer position if it is playing and updates
// the HitIndicator applet. The other timer watches the HitIndicator and
// repositions the audio/video stream if there was a change to it. Pretty
// simple, eh?
//
// The fun stuff that really makes one question one's sanity is that there
// appears to be a slight chance that when a window is reloaded or closed, we
// could be left holding an invalid reference to the RealPlayer plug-in, and
// calling one of its routines after that happens causes the browser to crash
// and burn. As a result, I've written the routines below in such a way as to
// (hopefully) minimize this chance. I hold no reference to a browser object
// longer than I really need to; this causes repeated lookups in a JavaScript
// document's embeds or applets array, but this is a minor annoyance compared
// to a browser crash. Just keep in mind if changing, or if you think you can
// pare down the code to really succinct routines. Been there, done that,
// crashed.
//

var gMonitorTimer = null;          // Reference to a window timer we create
var gRunning = false;              // Indicator of we are running or not

//
// GetRealPlayer -- returns a reference to the RealPlayer plug-in installed
// in this web page. Returns NULL if not running or if the plug-in no longer
// exists.
//
function GetRealPlayer()
{
      return gRunning ? document.embeds[ gRealPlayerName ] : null;
}

//
// GetHitIndicator -- returns a reference to the HitIndicator applet installed
// in this web page. Returns NULL if not running or if the applet no longer
// exists.
//
function GetHitIndicator()
{
      return gRunning ? document.applets[ gHitIndicatorName ] : null;
}

//
// HitIndicatorGetPending -- returns any pending position change posted by
// the applet because of a user's mouse click. Returns -1 if no change has been
// posted (or if the applet does not exist). Accesses a public variable slot in
// the main class of the applet.
//
function HitIndicatorGetPending()
{
      var obj = GetHitIndicator();
      return obj ? obj.getPending() : -1;
}

//
// RealPlayerSetPosition -- tell the RealPlayer plug-in to move to the given
// position in the audio stream. Starts the player if it is not already
// playing.
//
function RealPlayerSetPosition( pos )
{
      var obj = GetRealPlayer();
      if ( obj ) {
           if ( ! obj.CanStop() ) obj.DoPlay();
           obj.SetPosition( pos );
      }
}

//
// RealPlayerIsPlaying -- returns TRUE if the RealPlayer plug-in exists and is
// currently in a running state; FALSE otherwise.
//
function RealPlayerIsPlaying()
{
      var obj = GetRealPlayer();
      return obj ? ( obj.GetPlayState() == 3 ) : false;
}

//
// RealPlayerGetPosition -- returns the RealPlayer's current position in the
// audio/video stream, or -1 if the plug-in does not exists.
//
function RealPlayerGetPosition()
{
      var obj = GetRealPlayer();
      return obj ? obj.GetPosition() : -1;
}

//
// HitIndicatorSetPosition -- tell the applet to move its indicator to the
// given position.
//
function HitIndicatorSetPosition( pos )
{
      var obj = GetHitIndicator();
      if ( obj ) obj.setPosition( pos );
}

//
// Monitor -- timer routine that looks for a position change posted by the
// HitIndicator java applet and forwards it to the RealPlayer plug-in. Also
// feeds position changes from the RealPlayer back to the applet.
//
function Monitor()
{
      if ( ! gRunning ) return;
      var pos = HitIndicatorGetPending();
      if ( pos != -1 ) {
           RealPlayerSetPosition( pos );
      }
      else if ( RealPlayerIsPlaying() ) {
           pos = RealPlayerGetPosition();
           if ( pos != -1 ) HitIndicatorSetPosition( pos );
      }
}

//
// Install -- create a timer to periodically call our Monitor function. Do so
// every 10th of a second.
//
function Install()
{
      gRunning = true;
      gMonitorTimer = window.setInterval( "Monitor()", 100 );
}

//
// Uninstall -- let Monitor know that we are no longer active and remove our
// timer.
//
function Uninstall()
{
      if ( gRunning ) {
           gRunning = false;
           window.clearInterval( gMonitorTimer );
           gMonitorTimer = null;
      }
}

 >%--->%--->%--->%--->%--->%--->%--->%--->%--->%--->%--->%--->%---



*******************************************************
The RealForum is an email discussion group focused on using RealNetworks
products. The RealForum is a place to post messages about the best methods
for creating content using RealNetworks technologies and the planning and
implementation of streaming-media web sites.  Archives of RealForum can
be found at http://realforum.real.com

If you ever want to remove yourself from this mailing list,
you can send mail to <[EMAIL PROTECTED]> with the following
command in the body of your email message:

    unsubscribe realforum

or from another account, besides the address you subscribed with:

    unsubscribe realforum <[EMAIL PROTECTED]>

Reply via email to