|
Playing sound using Javascript
There are different ways to play sound using Javascript. A couple of examples. Example 1 Here's the simplest cross-browser script for playing
a sound file:
<a onclick="self.location='your_sound_file'; return false" href="#">Click here
to play</a>
Example: Click
here to play. The browser will open a separate sound control
window. The user will have to close this window manually. Note that
the script in Example 1 is not user-friendly: every time the user clicks to run
this script, the browser will open a new sound control window,
even if there might be a couple of other audio windows already playing the same
file. Example 2
Here's an example in which you won't see a separate sound control window.
In MS Internet Explorer, there are several ways to play a
sound file using Javascript, without opening a separate window for sound control.
For example, you can use the IE BGSOUND tag:
<bgsound id="bgsound_id" loop=1 volume="-600" src="images/jsilence.mid">
Use the following JavaScript code to play a sound file my_sound.mid
via the BGSOUND tag:
To start playing: document.all['BGSOUND_ID'].src='my_sound.mid'
To stop playing: document.all['BGSOUND_ID'].src='images/jsilence.mid'
Here my_sound.mid
stands for the name of the sound file that you
actually want to play. The images/jsilence.mid file does not play any sound at all, but can be
used to stop the playback of other sound files.
The source code of Example 2 is shown below:
<BGSOUND id=BGSOUND_ID src="">
<bgsound id="bgsound_id" loop=1 src="images/jsilence.mid">
<embed name="guitar" src="malaguena.mid" loop=false autostart=false hidden=true mastersound>
<script language="JavaScript">
<!--
ver=parseInt(navigator.appVersion)
ie4=(ver>3 && navigator.appName!="Netscape")?1:0
ns4=(ver>3 && navigator.appName=="Netscape")?1:0
ns3=(ver==3 && navigator.appName=="Netscape")?1:0
function playSound() {
if (ie4) document.all['BGSOUND_ID'].src='malaguena.mid';
if ((ns4||ns3)
&& navigator.javaEnabled()
&& navigator.mimeTypes['audio/x-midi']
&& self.document.guitar.IsReady()
)
{
self.document.guitar.play()
}
}
function stopSound() {
if (ie4) document.all['BGSOUND_ID'].src='images/jsilence.mid';
if ((ns4||ns3)
&& navigator.javaEnabled()
&& navigator.mimeTypes['audio/x-midi']
)
{
self.document.guitar.stop()
}
}
//-->
</script>
<form name=myform>
<input type=button value="Play Sound" onClick="playSound()">
<input type=button value="Stop Sound" onClick="stopSound()">
</form>
In Netscape Navigator, you'll have to use the LiveAudio plugin. To do so, you'll need to put an EMBED tag
in your page. For
example, if you want to play the file my_sound.mid and hide the
LiveAudio window, use the following EMBED tag: <embed name="my_sound"
src="my_sound.mid" loop=false autostart=false hidden=true
mastersound>
Use the following JavaScript code to control sound in Netscape:
To start playing: document.mySound.play()
To stop playing: document.mySound.stop()
See also
Music player
scripts
Using HTML to
display sound plugins with Netscape
Changing your event sounds
To change your event sounds on a PC:
From the 'Control Panel' (Start Menu > Settings) select 'Sounds' (or 'Sounds
and Audio Devices' depending on the Windows version). You are now in the 'Sounds Properties'
box, and those are your currently selected event sounds. From the 'Events' list choose an event e.g. Start Windows, New Mail Notification etc. then in the 'Sound' list use the
'Browse' to locate the sound you downloaded. Then click OK. Your sound should now be triggered by the event.
If you have a folder called 'sounds', it will always be easy to find your
Wav files, or you can copy the new Wav files in the Media folder in your Windows directory.
Not all e-mail programs have an e-mail sound notification. The only ones I am sure of that have one are AOL
("You've got mail" in your sound properties) and Microsoft Outlook Express
("New mail notification" in your sound properties). If you can't figure out your
e-mail sound or are unsure if your e-mail client even has one, try looking in the Help of your
e-mail client.
To change your event sounds on a Mac:
For Mac users it's a little more difficult. The only event sound that can be re-assigned is the Alert Beep. To assign sounds to
e-mail, start up, shut down, etc., you will need additional event sound software on your machine. You'll have to
search the web.
To use a sound as your Alert Beep, it must be placed in your 'Sound folder.' This file is located in your System Folder. To install a sound
first quit all applications. Download the
WAV by holding down the Control key and clicking on the link. Choose the option 'Download link to disk' and save the file to the desktop.
Then drag the sound file from the desktop to your Sound folder and select it from the selection of
WAV files there as your alert beep. For more details go to the section in Help called 'Changing the alert
sound'.
|