On-line Guides

How To Guides






The JavaScript FAQ
Prev Home Next

Stopping Sound

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 user 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. [埋込みオブジェクト:Bach.mid]

Let's assume that Microsoft Internet Explorer is playing the sound file mySound.mid specified by the BGSOUND tag
<BGSOUND ID="BGSOUND_ID" SRC="mySound.mid">
To stop playing the sound, you can use this code:
document.all['BGSOUND_ID'].src='jsilence.mid'
Here jsilence.mid is a "do-nothing" sound file - it does not play any sound at all.

Now assume that Netscape Navigator is playing the sound mySound.mid specified by the following EMBED tag:

<EMBED NAME="mySound" SRC="mySound.mid" 
LOOP=FALSE AUTOSTART=FALSE HIDDEN=TRUE MASTERSOUND>
Then you can use the following code to stop the playback:
document.mySound.stop()

JavaScripter.net. Copyright © 1999-2006, Alexei Kourbatov

The JavaScript FAQ
Prev Home Next


Mirrored with kind permission of Alexei Kourbatov Design by Interspire

AltStyle によって変換されたページ (->オリジナル) /