Page 248 - DCAP303_MULTIMEDIA_SYSTEMS
P. 248
Multimedia Systems
notes 3. Using a Background sound and Javascript
It is possible to do much the same as the above without the use of a hidden player if the browser
supports the BGSOUND tag (in practice only Internet Explorer). Basically background sounds
play as soon as the source sound is available; so the trick is to specify the source only in response
to a JavaScript function. Here is the BGSOUND tag and the function:
<bgsound id=”sound”>
<script>
function PlaySound(url) {
document.all.sound.src = url;
}
</script>
Here are examples of a link, an image and a button calling the function.
<a href=”#” onMouseOver=”PlaySound(‘success.wav’)”>Move mouse here</A>
Move mouse here.
<img src=”play.gif” onClick=”PlaySound(‘success.wav’)”>
<form>
<input type=”button” value=”Play Sound” onClick=”PlaySound(‘success.
wav’)”>
</form>
4. Using Dynamic HtML
Dynamic HTML allows you to use JavaScript to write new HTML code into your page and let it be
interpreted by the browser. The trick to using dynamic HTML for sound replay is to write into a
region of the document the HTML of an embedded sound set to automatically start replay on load.
Here we are going to use a <span> region and write into it using its “innerHTML” attribute:
function DHTMLSound(surl) {
document.getElementById(“dummyspan”).innerHTML=
“<embed src=’”+surl+”’ hidden=true autostart=true loop=false>”;
}
We can now create the dummy span region and just pass the URL of the sound file to the function
to play it:
<span id=dummyspan></span>
<form>
<input type=”button” value=”Play Sound” onClick=”DHTMLSound(‘success.
wav’)”>
</form>
5. Using a flash audio player
Although this method requires the Flash plug-in, that does seem to be quite commonly installed.
There are a number of Flash players available, but many of them do not support WAV format
files.
The latest version of the standalone player files can be downloaded from https://github.com/
francois2metz/WavPlayer.
Copy the file wavplayer.swf to your Web folder.
242 LoveLy professionaL University