JavaScript BackGround Sound


Background sound plays when the page is loaded. The two main types
of files are MIDI and WAVE.

The MIDI file is digital and a lot smaller than WAVE. MIDI has little
code to tell the sound card what note to play and what instrument
(from a voice table), and when to stop. Most are called "filename.mid"

The WAVE files (including MIDI with WAVE samples) are much larger,
work by "sampling" many times a second, sound from an external
source such as a microphone. The most common for web pages are
called filename.wav. There are lots more of them that may not be
supported by the visitor's browser such as: MIDI+SDS, SBK, SF2,
MOD, XM, RA, IWave, MP3.


In either case the filename.mid (or what ever) must be in the same
place as the HTML file. Otherwise it must include the Hypertext
Transfer Protocol (http) prefix. For example in my case:

angel.mid (if not in the current directory) must be called as:

http://www.waltm.net/angel.mid (which is where it is on my domain)

Anyway the main ways to get background music on a web page are

Note the "Value" or "LOOP" which tells how many times to play it.

------- The HTML way ----

<BGSOUND SRC="angel.mid" LOOP="infinite" >
<EMBED SRC="angel.mid" HEIGHT="2" WIDTH="2" AUTOSTART="true" HIDDEN="true" LOOP="true" >

To play once replace infinite with 1 and LOOP="True" with Loop="1"
The same goes for repeating an other number of times. Even the best sound
can get tired (Especially if the viewer has no way to shut it off)


<BGSOUND SRC="angel.mid" LOOP="1" >
<EMBED SRC="angel.mid" HEIGHT="2" WIDTH="2" AUTOSTART="true" HIDDEN="true" LOOP="true" >

------- The JavaScript way ----

Why use JavaScript when regular HTML tags work just fine.

Well when testing on the hard drive browsers use "file" protocol instead of
"http" (HyperText Transfer Protocol) which usually won't work on sound files.


<Script Language="JavaScript">
<!--
if (navigator.appName == "Netscape") {
document.write( "<embed src='angel.mid' autostart='true' repeat='5'>");
} else {
document.write( "<bgsound src='angel.mid' loop='5'>");
} // -->
</Script>

  • Sound Files On My Pages.