Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
396 views
in Technique[技术] by (71.8m points)

javascript - How to add background music to a web page?

How do I add background music to a web page? So that when the visitor opens the page, the music will auto play.

I have tried <object> <embed> and <bgsound> but they are all not working in firefox. Why?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The <bgsound> tag is Internet Explorer-specific and will thus not work in other browsers such as FireFox. The <embed> tag should work in FireFox if you use it correctly. It will use a browser plug-in to play the sound. Below is an example:

<embed loop="true" src="sound.wav" hidden="true" type="video/quicktime"></embed>
  • loop="true" specifies to play the sound repeatedly.
  • src="sound.wav" specifies the relative path of the sound file to play. The variety of formats you can play depends on what type= you specify.
  • hidden="true" indicates to not show the media player's interface. Hide it if you want the user to not be able to pause, stop, or navigate through the sound.
  • type="video/quicktime" specifies to use a Quicktime component, which means the client must have Quicktime installed. Use application/x-mplayer2 for Windows Media Player or audio/x-pn-realaudio-plugin for Real Player audio. Quicktime plays more formats and is probably what you will want to use.

Alternatively, use <object> in a very similar way. An example is below:

<object data="sound.wav" type="video/quicktime" width="0" height="0">
    <param name="filename" value="sound.wav">
    <param name="autostart" value="1">
    <param name="playcount" value="true"> 
</object>

Keep in mind that, like the <marquee> tag, background sound on a web page is generally frowned upon because it is often obtrusive and annoying. Also, as the user switches between pages or causes post-backs, the sound will restart from the beginning. Only use audio formats that are highly compressed, meaning they have small file sizes, or the sound will not play for several seconds while it downloads to the client machine.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...