JavaScript Tip of the Week for September 9, 1996: Much Ado About LiveConnect | 3 | WebReference

JavaScript Tip of the Week for September 9, 1996: Much Ado About LiveConnect | 3

JavaScript Tip of the Weekfor September 16, 1996: JavaScript and Plugins

With LiveConnect, JavaScript can call a plug-in's functions and access its variables. Below is an example of JavaScript calling the LiveAudio plug-in's functions. You can easily start, stop, and pause the plug-in with only a small amount of code. Please wait for a moment while the sound files load or read on. ( This is and always will be a problem until everybody gets a T1. )
Unlike the Aug. 26 sound playing tip, this one does not need frames to work. It also has two other advantages: you can play more than one sound file at a time and you can preload the sounds so they will play as soon as you call them. Think about it, you can now fully integrate souund into your web page. How is all this done? Using the embed tag, LiveAudio, and a few lines of JavaScript.

First, create LiveAudio embeds for all of the sounds you want to be played. In the case of my little app, you need two music files and one AU file. There are two special parameters that these embeds need, HIDDEN and NAME:
    <EMBED 
     SRC = "classic.mid" 
     HIDDEN = TRUE
     CONTROLS = console
     VOLUME = 100 
     LOOP = FALSE 
     AUTOSTART = FALSE
     NAME = "classic" 
     MASTERSOUND>
The HIDDEN parameter does just what it says; it hides the LiveAudio control. The name parameter is important because when you start/pause/stop the sound, you need to refer to it by name. (Note: Embeds are also indexed as arrays, but its more convenient do control them with names and it's also the method Netscape recommends.) You can add as many embeds as you want, just give them all different names; prefererably descriptive ones. I named my app's controls classic, jazz, and click. Now to control the embed, just use these three commands:
    document.embed_name.play([true/false]); <-- Play Sound
    document.embed_name.stop();             <-- Stop Sound
    document.embed_name.pause();            <-- Toggle Pause
Where embed_name is the name of the embed you want to control. The true/false parameter in play() determines whether or not the sound should be repeated. Just so you know, these functions are not defined by any script, they are defined by the plugin. No extra code is needed to control the sounds, that's the whole essence of LiveConnect. Finally, as a "saftey precaution" add this if statement to make sure the sounds are loaded before an attempt at playing them is made:
    if (document.embed_name == null           || 
        document.embed_name.IsReady() != true  )
    {
    alert("Please wait for a moment while the sound files load.");
    history.go(0);
    return;
    }
All of this can be combined with onMouseovers and other events to create some pretty 'loud' pages. Hope you find this as useful as I have, you'll see all of this code used in an upcoming JavaScript Tip of the Week Project.

Source

Note: Below are a few more functions that you can use to control LiveAudio, though some of them will only work on the Win 95 and Mac platforms.


setvol(in percent) Sets the volume of the specified embed in percent.
start_time(in seconds) Sets the point at which the embed should start in seconds.
end_time(in seconds) Sets the point at which the embed should end in seconds.
fade_to(in percent) Sets the percent that the embed should fade to.