21. Voice
1. a browser that can play a sound (Audio-enabled browsers)
Q:Which browser can execute the script for playing sound?
A:If the technology of sound playback description can work in miscrosoft Internet Explorer 4.0 and later versions, it can also work in Netscape Navigator 3.0 and later versions. To play a sound in Internet Explorer, testing in version 4.0 and later is sufficient. Then in Netscape Navigator, make sure the following:
- The browser version is 3.0 or later.
- Enable Java (not just JavaScript !)
- The browser can recognize the MIME type of the audio file to be played
- Liveaudio plug-in available
For example, if you want to play the audio file mysound. Mid in the following EMBED tag
<EMBED NAME="mySound" SRC="mySound.mid" mce_SRC="mySound.mid" LOOP=FALSE AUTOSTART=FALSE HIDDEN=TRUE MASTERSOUND>
The following code is required for testing:
ver=parseInt(navigator.appVersion) if (ver>2 && navigator.appName=="Netscape" && navigator.mimeTypes['audio/x-midi'] && navigator.javaEnabled() && document.mySound.IsReady() ) { // put Netscape-specific code here } if (ver>3 && navigator.appName.indexOf("Microsoft")!=-1) { // put Explorer-specific code here }
2. Audio File Format (audio file formats)
Q:What types of audio files can be played on a webpage?
A:All open-source audio viewers support audio files in wavelet and midiformat (.mid).midiand .wav files. Depending on your browser and system configuration, other format files may or may not be supported. For example, readaudio can only be played back after you install RealPlayer.
3. Playing sound from Javascript)
Q:How can I write a Javascript script for playing sound?
A:There are several different ways to play the sound through JavaScript. Let's look at several examples:
Example 1.This is a simple cross-browser script for playing sound files:
self.location="AUDIO_FILE_URL"
Click here to test sample 1 code. The browser opens a separate sound control window. You need to close the window manually. Note: Example 1 is unfriendly to users: each time you click to run the script, the browser opens a new sound control window, even if there is already an audio window that is playing the same file!
Example 2.This is a more complex example. In this example, there is no separate sound control window. (This example is discussed below .)
InMicrosoft Internet Explorer. For example, you can use the IE-specific bgsound Tag:
<BGSOUND ID="BGSOUND_ID" LOOP=1 VOLUME="-600" SRC="jsilence.mid" mce_SRC="jsilence.mid">
Use the following JavaScript code to use the bgsound tag to play the audio file mysound. Mid:
// Start to play the document. all ['bgsound _ id']. src = 'mysound. mid '// stop playing the document. all ['bgsound _ id']. src = 'jsilence. mid'
Here mysound. mid indicates the name of the audio file to be played; jsilence. mid is a "do nothing" sound file-it does not play any sound, but can be used to stop playback of other sound files.
InNetscape NavigatorYou must use the liveaudio plug-in. Therefore, you need to put an EMBED tag on the page. For example, if you want to play the mysound. Mid file and hide the liveaudio window, you can use the following EMBED tag:
<EMBED NAME="mySound" SRC="mySound.mid" mce_SRC="mySound.mid" LOOP=FALSE AUTOSTART=FALSE HIDDEN=TRUE MASTERSOUND>
Use the following JavaScript code to control the sound in Netscape:
// Start playing document. mysound. Play () // stop playing document. mysound. Stop ()
Sample 2 codeAs follows. The code first tests whether the browser enables audio and then uses the technology described on this page. You will see two JavaScript Functions: playsound () and stopsound (), which start and stop playback.
<BGSOUND id="BGSOUND_ID" LOOP=1 SRC="jsilence.mid" mce_SRC="jsilence.mid"><EMBED NAME="Bach" SRC="Bach.mid" mce_SRC="Bach.mid" LOOP=FALSE AUTOSTART=FALSE HIDDEN=TRUE MASTERSOUND><mce:script language="JavaScript"><!--ver=parseInt(navigator.appVersion)ie4=(ver>3 && navigator.appName!="Netscape")?1:0ns4=(ver>3 && navigator.appName=="Netscape")?1:0ns3=(ver==3 && navigator.appName=="Netscape")?1:0function playSound() { if (ie4) document.all['BGSOUND_ID'].src='Bach.mid'; if ((ns4||ns3) && navigator.javaEnabled() && navigator.mimeTypes['audio/x-midi'] && self.document.Bach.IsReady() ) { self.document.Bach.play() }}function stopSound() { if (ie4) document.all['BGSOUND_ID'].src='jsilence.mid'; if ((ns4||ns3) && navigator.javaEnabled() && navigator.mimeTypes['audio/x-midi'] ) { self.document.Bach.stop() }}// --></mce:script><form name=myform><input type=button value="Play Sound" onClick="playSound()"><input type=button value="Stop Sound" onClick="stopSound()"></form>
4. Stop sound)
Q:How do I compile a JavaScript function for stopping sound playback?
A:If you use self. Location = "audio_file_url" (see example 1) to open a separate window, there is no simple program to stop the sound. You can only close the window manually.
To stop playing audio using JavaScript, you need to use the technology described in example 2.
Let's assume thatMicrosoft Internet ExplorerUse a specific bgsound tag to play the audio file mysound. mid.
<BGSOUND ID="BGSOUND_ID" SRC="mySound.mid" mce_SRC="mySound.mid">
To stop playing a video, use the following code:
document.all['BGSOUND_ID'].src='jsilence.mid'
Here, jsilence. Mid is a sound file that does nothing-it does not play any sound.
Now suppose,Netscape NavigatorUse the embed tag to play the audio file mysound. Mid:
<EMBED NAME="mySound" SRC="mySound.mid" mce_SRC="mySound.mid" LOOP=FALSE AUTOSTART=FALSE HIDDEN=TRUE MASTERSOUND>
Then you can use the following code to stop playback:
document.mySound.stop();
5. Playing Sound onmouseover)
Q:Can I play a sound when an onmouseover event occurs?
A:Yes. You can call the sound playback function in the hyperlink's onmouseover event processor. Try the following example: When the mouse pointer goes through the hyperlink below, the browser starts playing the sound.
| J. S. Bach's suite in B-minor |
This is the source code of the hyperlink:
<a class=playOnHoverhref=# title="This hyperlink plays a sound onMouseOver."onClick="return false"onMouseOver="playSound();return true"onMouseOut ="stopSound();return true"> J.S.Bach's Suite in B-minor </a>
For the codes of the playsound () and stopsound () functions, see the audio playback section.
6. preloading sound files)
Q:How can I pre-load a sound file?
A:In Netscape Navigator 3.0 and later versions, Microsoft Internet Explorer 4.0 and later versions, you can use the following ebmed tag to pre-load a sound file mysound. Mid:
<EMBED NAME="mySound" SRC="mySound.mid" mce_SRC="mySound.mid" LOOP=FALSE AUTOSTART=FALSE HIDDEN=TRUE MASTERSOUND>
Then, you can use the sound playback technology to play the file.
7. Background sound)
Q:How do I set the background sound of a webpage?
A:You can use the embed label to play background sound on all browsers that enable sound. For example, if you want to play the bkground. Mid file after the browser loads the page completely, you can use the following EMBED tag:
<EMBED NAME="bkgroundID" SRC="bkground.mid" mce_SRC="bkground.mid"LOOP=TRUE AUTOSTART=TRUE HIDDEN=TRUE MASTERSOUND>
To stop the background sound, you can call the cross-browser method:
document.bkgroundID.stop();
If your target browser isMicrosoft Internet Explorer(For example, in the Intranet), you can use the bgsound label dedicated to IE:
<BGSOUND ID="bkgroundID" LOOP=0 VOLUME="-600" SRC="bkground.mid" mce_SRC="bkground.mid">
Bkground. Mid indicates the name of the audio file to be played.
8. Playing Sound without Javascript is not used)
Q:Can I play audio files without using JavaScript?
A:Yes. You can specify the URL of the sound file in a hyperlink to play the sound, for example, <a href = "mysound. Mid"> (click to observe the effect ).
When you click this link, the browser opens a separate sound control window. Then you must close the window manually. Even worse, each time you click, a new window is opened, even if several windows are playing the same file.