Stopping the Sound Playback

JavaScript FAQ | JavaScript Sound FAQ  

Question: How do I write a JavaScript function that stops playing sound?

Answer: If you've opened a separate sound control window using the code self.location="AUDIO_FILE_URL" (see Example 1), then there is no simple way to stop the sound programmatically. The users will have to close the sound control window manually.

To be able to stop playing sound from JavaScript, you'll need to use the techniques described in Example 2. Let's assume that Microsoft Internet Explorer is playing the sound file mySound.mid specified by the BGSOUND tag

To stop playing the sound, you can use this code:
Here jsilence.mid is a "do-nothing" sound file – it does not play any sound at all.

In Firefox, Safari, or Google Chrome, in scenarios that rely on an IFRAME containing a plugin for sound playback, you can stop the playback by overwriting or replacing the IFRAME location, e.g. using location.replace() as shown in Example 2 and here:

This function combines the above ways to stop sound playback:

function stopSound() {
 if (document.all) document.all['BGSOUND_ID'].src='jsilence.mid';
 else self.iplayer.location.replace('jsplayer.htm?stop');

(For a full listing of the source code, see Example 2.)

Historical note: In Netscape Navigator, in order to stop the playback of the sound file mySound.mid specified by the following EMBED tag:

<EMBED NAME="mySound" SRC="mySound.mid" 
you could use the method call: document.mySound.stop();
(This may or may not work in newer browsers!)

See also:

  • How do I write a JavaScript function that plays a sound file?
  • Can I play a sound file without using JavaScript?
  • Copyright © 1999-2008,