js实现可兼容IE、FF、Chrome、Opera及Safari的音乐播放器_javascript技巧

WBOY
Release: 2016-05-16 16:14:39
Original
1001 people have browsed it

本文实例讲述了js实现可兼容IE、FF、Chrome、Opera及Safari的音乐播放器。分享给大家供大家参考。具体实现方法如下:

/** 音乐播放器 * @param obj 播放器id * @param 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('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); } } }
Copy after login

用法示例:

var file = []; file['mp3'] = '1.mp3'; file['ogg'] = '1.ogg'; audioplayer('audioplane', file, true); // 播放 audioplayer('audioplane'); // 停止
Copy after login

希望本文所述对大家的javascript程序设计有所帮助。

Related labels:
source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!