![]() ![]() ![]() ![]() |
![]() |
Chapter Twenty Playing and Controlling Sound Files from JavaScript |
Deciding Which Sound Files to Use One of the ways to make a page at your site more exciting for visitors is to add sound. There are many functions that you can control from JavaScript pertaining to that sound. The problem, of course, is how confoundedly big those darn sound files can be. A minute or two of digitized sound can quickly rise to a megabyte or more of download -- clearly impractical for all but the most dedicated dial-up user. Real Audio® and others have developed "streaming audio" that addresses this problem somewhat, and for voice, this approach is probably the best technology to use. Music is another situation altogether. Although you could still use your .au and .wav files for this purpose -- again, they are really big. An "old" technology on the web, however, offers a very pleasing solution to providing music without huge downloads. That is the ubiquitous MIDI file. The folks at Netscape® have developed LiveAudio® and the folks at Apple® have developed a QuickTime® Instrument library that "lives" on your computer. And this is no minimalistic library. Composers can pick from such esoteric instruments as bagpipes, shagahuchi, kogo, metal drums, sci-fi and more, in addition to more conventional instruments. Why the sales pitch? Simple. A 60 second playing time MIDI file with lots of channels of instrumentation and orchestration may require less than 4 or 5k of data. You read that right. That is a tiny file! (A corresponding .au or .wav file would easily consume 200 to 500k of download) Why should you care? The dial-up visitor to your pages with, say, a 14.4 modem can receive the MIDI file in 2 or 3 seconds, while the bigger sound files will take 2 to 5 minutes under optimum net conditions. It's your call, of course, but using unnecessary extra bandwidth will cost you in more ways than one. The control of sound files from JavaScript won't change, regardless of your decision. The script doesn't care if you call a .mid, an .aif, an .au or a .wav file, etc. Refamiliarizing Yourself With the HTML Calls Needed for Playing Sound Files Yeah, I know. You are so doggone familiar with HTML that you already know this stuff that follows. Why am I wasting your time with HTML for audio? Well, because the truth is that you haven't used it much and may have "forgotten" some of it. Audio Controls If you specify a direct link to a sound file, Netscape LiveAudio® opens a new window by default. It is a tiny window 60 pixels high by 144 pixels wide, that exactly contains the Console. You don't have to specify anything. If the user has another plug-in configured, such as Crescendo!®, the visual will change, but not the dimensions. That HTML is all that is needed. If the user clicks on the link, everything happens automatically. The CONSOLE contains a start button, a stop button, a pause button and a volume control slider. |
Big Console HEIGHT=60 WIDTH=144
Big Console in Automatically Opened Window
There is also a more compact console that I prefer to use, which is referred to in HTML as "smallConsole". It does not include a pause button, but does contain the play, stop and volume slider controls. |
Small Console HEIGHT=15 WIDTH=144
Although it would seem intuitive what the buttons control on the smallConsole, a bit of confusion on the part of some users has led me to always include a small font size "legend" on the line below the smallConsole to "describe" the functions of the buttons: |
Small Console with added Legend Line
The HTML calls to use in the <EMBED SRC=etc. tag. To display the consoles on your own pages is similar to calling any other image. The difference is the <EMBED SRC= tag. ALIGN=position As with image calls, you can specify the alignment position of the console images with ABSBOTTOM, ABSMIDDLE, BASELINE, BOTTOM, CENTER, LEFT, MIDDLE, RIGHT, TEXTTOP or TOP. AUTOSTART=TRUE will cause the sound to begin as soon as the sound file has been loaded. You may use the AUTOSTART=FALSE call if you wish, but it is totally unnecessary, since this is the default. If you leave the call AUTOSTART out of your tag, it won't start playing until you call it from JavaScript or the user clicks the Play button. CONTROLS=CONSOLE will cause the big console to display, CONTROLS=smallConsole will display the small one. There are also calls to display individual components of the console. The calls for these are: PLAYBUTTON, PAUSEBUTTON, STOPBUTTON and VOLUMELEVER respectively. See the section on custom consoles below for a bit more detail. HEIGHT=60 - CONSOLE HEIGHT=15 - smallConsole (These are default heights, so need not always be specified. However, for some reason, on some platforms, it helps to specify heights. Notice that there are no quotes around the numbers. Yeah, the HTML specs say there should be quotes -- but don't use 'em in your EMBED calls) HIDDEN=TRUE is specified only if you want no display of a controller on screen. Caution: do not use HIDDEN=FALSE, since even though it shouldn't matter, because this is the default, it is a definitely bad call that may cause errors. Note: although Microsoft Internet Explorer® will experience no problems with the HTML call <BACKGROUND SRC="my.mid"> Netscape® browsers may on some platforms. So the <EMBED SRC=etc. will probably serve you better for hidden sound files. LOOP=TRUE will cause your sound file to repeat endlessly once it has started playing. (If you have a console, the Stop button will override this call and stop the sound. But if the Play button is clicked again, the sound will again repeat endlessly until the Stop button is clicked) LOOP=FALSE is an unnecessary call, since it is the default. (When the Play button is clicked, the sound file will play through just once and stop automatically) There is one extra LOOP call that you can make: LOOP=x where x is any whole number integer. With this call, the sound file will be played the number of times you specify with x, and then will stop. WIDTH=144 - CONSOLE and SMALLCONSOLE (These are default widths, so need not always be specified. However, for some reason, on some platforms, it helps to specify widths. Notice that there are no quotes around the numbers) Combining the desired attributes might result in HTML like the following: Master Sound and Distributed Buttons In addition to the large and small consoles, LiveAudio® permits you to use the components of the consoles to create custom consoles. At the heart of this is the MASTERSOUND and NAME calls. Without going into great detail, you can control a number of sound files with just one volume control, etc., by giving them a common NAME such as "custom" and then adding the MASTERSOUND call to the volume lever, for example. This requires the use of so-called "stub" files, which is beyond the scope of this work. Visit Netscape® for details, if desired. However, if you wish to use individual buttons for your own console, here are the heights and widths: HEIGHT=22 - PAUSEBUTTON
WIDTH=37 - PAUSEBUTTON
VOLUME=x This call lets you specify the system sound volume - useful if you are not including a volume slide of one kind or another. x is any value from 1 to 100 (indicating per cent of maximum volume) If you don't call a volume=percentage, i.e., leave out the volume call altogether, the default is whatever the user currently has set his/her system volume to - probably the polite thing to do if you aren't giving your visitor a control. STARTTIME=00:00 (Minutes:Seconds) This call lets you delay the commencement of playing for a sound file. If, for example, you wanted to wait two and a half minutes, you would set it to 02:30 Note: at present, this is a valid call only on Macintosh®, Windows 95® and NT® platforms. ENDTIME=00:00 (Minutes:Seconds) This call lets you specify the duration of playing for a sound file. If, for example, you wanted it to play for two and a half minutes, you would set it to 02:30 Note: at present, this is a valid call only on Macintosh®, Windows 95® and NT® platforms. If the NAME=MASTERSOUND call is included with the VOLUMELEVER, it will control a number of different sound files on that page without displaying a volume lever for each. However, if several Consoles or Small Consoles appear on the same page, the built-in volume slide will control system sound, even if the lever which is moved is not presently "active" because a different sound file is playing in a different console. A Script Using the smallConsole Because changing selections on the present document screen would require a reload of the page, it is usually better to create a frameset document and simply direct the selections to a small pane of that frame. In addition, you may wish to "spool" sequential songs to the visitor's speakers, so that feature is also incorporated in the following parent and two child documents script. A link to see them follows the last. Parent Document - s31.htm First Child Document - s53.htm Second Child Document - s54.htm Click Here to See These Scripts. |