JavaScript FAQ (24)--Sound

Source: Internet
Author: User

21. Sound

1, can play the sound of the browser (audio-enabled Browsers)

Q: What can a browser do to play a sound script.

A: If the technology described in sound playback can work in Miscrosoft Internet Explorer 4.0 and its newer version, then you can also work in Netscape Navigator 3.0 and its newer version. To play a sound in Internet Explorer, testing is sufficient in 4.0 and later versions. And then in Netscape Navigator, you have to make sure that the following: Browser version is 3.0 or newer version Java enabled (not just JavaScript. Browser to identify the MIME type you want to play audio files LiveAudio plugin available

For example, if you want to play a sound file in the embed tag below Mysound.mid

<embed name= "Mysound" src= "Mysound.mid" mce_src= "Mysound.mid" 
    loop=false autostart=false Mastersound>


Then you need to test with the following code:

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 files Formats)

Q: Web pages can play those types of audio files.

A: All audio-enabled browsers support playback of audio files for WAV and MIDI formats (. Mid,. MIDI, and. wav files). Depending on the user's browser and system configuration, other format files may be supported or not supported. For example, Readaudio can be replayed only if the user installs the RealPlayer.

3. Use JavaScript to play sound (Playing Sound from JavaScript)

Q: How do I write a JavaScript script that plays sound?

A: There are several different ways to play sounds through JavaScript. Let's look at a few examples:

Example 1. This is one of the easiest cross-browser scripts to play sound files:

self.location= "Audio_file_url"

Click here to test the example 1 code. The browser opens a separate sound control window. Then the user needs to manually close the window. Note that Example 1 is not user-friendly: every time a user clicks on a run script, the browser opens a new sound control window, even if an audio window is already playing the same file.

Example 2. This is a more complex example, and this example does not have a separate sound control window. (The following is a discussion of this example.) )

In Microsoft Internet Explorer , there are several ways to play a sound file through JavaScript without opening a separate sound control window. 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 play the sound file through the Bgsound tab mysound.mid:

Start playing 
document.all[' bgsound_id '].src= ' mysound.mid ' 
/stop playing
document.all[' bgsound_id '].src= ' Jsilence.mid '

The Mysound.mid here represents the name of the sound file you actually want to play; Jsilence.mid is a "do nothing" sound file-it does not play any sound, but it can be played back by stopping other sound files.

In Netscape Navigator You have to use the LiveAudio plugin; therefore, you need to put a embed tag on the page. For example, if you want to play the file Mysound.mid 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 Mastersound>

Use the following JavaScript code to control sounds in Netscape:

Start playing 
document.mySound.play () 
/stop playing 
document.mySound.stop ()

the code for example 2As follows. The code first tests whether the browser has audio enabled, and then uses the technology described by the page. You'll 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 "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= ' 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= ' Jsilenc '
 E.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 (stopping Sound)

Q: How to write a JavaScript function that stops 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. Users can only close windows manually.

To be able to stop sound playback through JavaScript, you need to use the technique described in Example 2.

We assume that Microsoft Internet Explorer plays a sound file through a specific bgsound tag Mysound.mid

<bgsound id= "bgsound_id" src= "Mysound.mid" mce_src= "Mysound.mid" >

To stop playing, you can use code:


The jsilence.mid here is a "do nothing" sound file-it does not play any sound.

Now assume thatNetScape Navigator uses the embed tag to play a sound file Mysound.mid:

<embed name= "Mysound" src= "Mysound.mid" mce_src= "Mysound.mid" 
loop=false autostart=false Mastersound>

You can then use the following code to stop playback:

Document.mySound.stop ();

5, OnMouseOver event playback sound (Playing Sound onMouseOver)

Q: can I play a sound when the onmouseover event occurs?

A: That 's OK. You can call the sound playback function in the onmouseover event handler of the hyperlink. Try the following example: When the mouse pointer passes through the hyperlink below, the browser starts playing the sound.

J.S.Bach ' S Suite in B-minor

This is the source code for the hyperlink:

<a class=playonhover
href=# 
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>

The code for the function PlaySound () and Stopsound () is shown in the Sound playback section.

6. Pre-loading sound file (preloading Sound files)

Q: How do i preload a sound file?

A: in Netscape Navigator 3.0 and later versions and Microsoft Internet Explorer 4.0 and later versions, You can use the following ebmed tag to preload a sound file Mysound.mid:

<embed name= "Mysound" src= "Mysound.mid" mce_src= "Mysound.mid" 
loop=false autostart=false Mastersound>

You can then use the Sound playback section of the technology to play the file.

7. Background sound (Background Sound)

Q: How to set the background sound of a Web page.

A: on all enabled browsers, you can use the Embed tab to play the background sound. For example, if you want to play the file Bkground.mid after the browser has completely loaded the page, you can use the following embed tag:

<embed name= "Bkgroundid" src= "Bkground.mid" mce_src= "Bkground.mid"
loop=true autostart=true Mastersound>

To stop a background sound, you can invoke a Cross-browser method:

Document.bkgroundID.stop ();

If your target browser is Microsoft Internet Explorer(for example, in the intranet), then you can use the IE-specific bgsound tag:

<bgsound id= "Bkgroundid" loop=0 volume= " -600" src= "Bkground.mid" mce_src= "Bkground.mid" >

Again, Bkground.mid represents the name of the sound file you actually want to play.

8. Do not use JavaScript to play sound (Playing Sound without JavaScript)

Q: can I play a sound file without using JavaScript?

A: That 's OK. You can specify the URL of a sound file in a hyperlink to play the sound, such as <a href= "Mysound.mid" > (click to observe the effect).

When the user clicks on the link, the browser opens a separate sound control window. The user must then manually close the window. Worse, every time a user clicks on a new window opens, even though several windows are already playing the same file.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.