This article describes the JS implementation compatible with IE, FF, Chrome, opera and Safari music players. Share to everyone for your reference. The implementation method is as follows:
/** Music player * @param obj player ID * @param file Audio file mp3:ogg: * @param loop whether loop/function Audioplayer (ID, file, lo
OP) {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 = Docume
Nt.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 (' Loop ', ' Loop ');
} 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[' ogg '];
Ogg.type= ' Audio/ogg ';
Player.appendchild (OGG);
}
}
}
Usage examples:
var file = [];
file[' mp3 '] = ' 1.mp3 ';
file[' ogg ' = ' 1.ogg ';
Audioplayer (' Audioplane ', file, true); Play
Audioplayer (' Audioplane ');/stop
I hope this article will help you with your JavaScript programming.