Home  >  Article  >  Web Front-end  >  Implement music playback js code compatible with various browsers

Implement music playback js code compatible with various browsers

小云云
小云云Original
2017-11-18 10:00:102653browse

Sometimes when we are making a web page. We need to use background music, but we didn't expect that some browsers can't play it at all. So what kind of js code can achieve this compatible function? If there is no such thing, the editor below will share with you the js code for background music loop playback that is compatible with various browsers.

var bgSoundFile = " 
if (navigator.userAgent.toLowerCase().indexOf("msie") != -1) 
{    document.write(&#39;<bgsound src="&#39; + bgSoundFile + &#39;" loop="-1">&#39;);
} else if (navigator.userAgent.toLowerCase().indexOf("firefox") != -1) 
{    document.write(&#39;<object data="&#39; + bgSoundFile + &#39;" type="application/x-mplayer2" 
width="0" height="0"><param name="filename" value="&#39; + bgSoundFile + &#39;">
<param name="autostart" value="1"><param name="playcount" value="infinite">
</object>&#39;);} 
else {    document.write(&#39;<audio src="&#39; + bgSoundFile + &#39;" autoplay="autoplay" loop="loop">
<object data="&#39; + bgSoundFile + &#39;" type="application/x-mplayer2" width="0" height="0">
<param name="filename" value="&#39; + bgSoundFile + &#39;"><param name="autostart" value="1">
<embed height="2" width="2" src="&#39; + bgSoundFile + &#39;" pluginspage="http://www.apple.com/quicktime/download/" 
type="video/quicktime" controller="false" controls="false" autoplay="true" autostart="true" loop="true" bgcolor="#000000">
</embed>
</object>
</audio>&#39;);
}

The above is the js code that can realize the background music loop playback compatible with various browsers. Maybe it can be used in the future, so hurry up and save it.

Related recommendations:

How to use html5 to write a web music player

JS button to add background music implementation code

Implementation example of Java music player

The above is the detailed content of Implement music playback js code compatible with various browsers. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn