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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | var bgSoundFile = "
if (navigator.userAgent.toLowerCase().indexOf( "msie" ) != -1)
{ document.write('<bgsound src= "' + bgSoundFile + '" loop= "-1" >');
} else if (navigator.userAgent.toLowerCase().indexOf( "firefox" ) != -1)
{ document.write('<object data= "' + bgSoundFile + '" type= "application/x-mplayer2"
width= "0" height= "0" ><param name= "filename" value= "' + bgSoundFile + '" >
<param name= "autostart" value= "1" ><param name= "playcount" value= "infinite" >
</object>');}
else { document.write('<audio src= "' + bgSoundFile + '" autoplay= "autoplay" loop= "loop" >
<object data= "' + bgSoundFile + '" type= "application/x-mplayer2" width= "0" height= "0" >
<param name= "filename" value= "' + bgSoundFile + '" ><param name= "autostart" value= "1" >
<embed height= "2" width= "2" src= "' + bgSoundFile + '" 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>');
}
|
Copy after login
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!