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

JavaScript Tip of the Week for September 16, 1996: Much Ado About LiveConnect

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.
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 on 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:
     SRC = "classic.mid" 
     CONTROLS = console
     VOLUME = 100 
     LOOP = FALSE 
     NAME = "classic" 
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:[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. LiveAudio is not the only LiveConnect enabled plugin, there are many out there now, such as Voxware Gold, and there there will be many more to come.

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.


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.