Lesson Six B

Sound files (.mp3 and .wav) can also be played automatically if the background sound (bgsound) is set in the head section of a Web page. Again you can just copy and modify the code I have used here to incorporate sound files into your own Web pages. There is no need for any file to play when the page is first loaded: just put src="" in the bgsound tag and it will do nothing (unless the JavaScript code changes the source when a button is pressed).

The following JavaScript code is optional: sound.loop="2"; sound.balance="-10000". You can cut it out and just put the curly bracket immediately after the .wav" bit. The balance value can be anything from -10000 to 10000 and it allows you to push the sound out to one side or other. The loop part allows you to make the background sound file play repeatedly: "0" makes it play once; "2" makes it play three times; -1 makes it play indefinitely. It is not necessary to use JavaScript to set these values for the original sound file: if you want to use loop or balance values other than zero (which is the default value for both), you can just put them in the bgsound tag in the following way: <bgsound src="sound-file.mp3" loop="-1" balance="10000">.

The volume can also be set in the bgsound tag by putting volume="-500" in it (or sound.volume="-500" in JavaScript, separated from other items by a semicolon). The value for volume can be anything from -10000 to 0, with zero being maximum volume and -10000 being the minimum: it should change the volume relative to the volume you have set the computer to at the time.