This article describes how to use javascript to enable music players compatible with IE, FF, Chrome, Opera, and Safari. You can use the custom javascript function audioplayer to enable music playing in common browsers, it is of great practical value. If you need it, you can refer to the example in this article to describe the Javascript music player that is compatible with IE, FF, Chrome, Opera, and Safari. Share it with you for your reference. The specific implementation method is as follows:
/** Music player * @ param obj player id * @ param file audio file mp3: ogg: * @ param loop */function audioplayer (id, file, loop) {var audioplayer = document. getElementById (id); if (audioplayer! = Null) {document. body. removeChild (audioplayer);} if (typeof (file )! = 'Undefined') {if (navigator. userAgent. indexOf ("MSIE")> 0) {// IE var player = document. createElement ('bgsound'); player. id = id; player. src = file ['mp3']; player. setAttribute ('autostart', 'true'); if (loop) {player. setAttribute ('loop ', 'infinite');} document. body. appendChild (player);} else {// Other FF Chome Safari Opera var player = document. createElement ('audio'); player. id = id; player. setAttribute ('autoplay', 'autoplay'); if (loop) {player. setAttribute ('login', 'login');} document. body. appendChild (player); var mp3 = document. createElement ('source'); mp3.src = file ['mp3']; mp3.type = 'audio/mpeg '; player. appendChild (mp3); var ogg = document. createElement ('source'); ogg. src = file ['ogc']; ogg. type = 'audio/ogy'; player. appendChild (ogg );}}}
Usage example:
Var file = []; file ['mp3'] = '1.mp3'; file ['ogc'] = '1.ogc'; audioplayer ('audioplane ', file, true ); // play audioplayer ('audioplane '); // stop
I hope this article will help you design javascript programs.