在浏览器中播放音频文件的兼容性问题

来源:http://blog.sina.com.cn/s/blog_96ae64bd0100zk9r.html
下面谈谈本人在html中插入音频文件,经过我的本地测试总结的一些问题(播放mp3文件):
 1、<embed type="audio/mp3" src="" autostart=true  loop=false></embed>
    问题:IE8上正常(通过media player插件来播放)但在IE6和IE7上不会播放
          Firefox上要安装QuickTime插件才能播放
          Chrome通过将其转化成html5上的<vidio>标签播放,能播放但会使整个屏幕蓝屏
          Opera不会自动播放
 2、<embed type="audio/midi" src="" autostart=true loop=false></embed>
    问题:IE6,IE7上不会正常播放,IE8正常
          Firefox上正常
          Chrome上要求肮脏QuickTime插件才能正常播放
          Opera不会自动播放
 3、<object data="" />
    问题:在IE6,7上不能播放,IE8会弹出“非正常使用的Articx”等字样的提示
          Firefox上正常
          Chrome上正常
          Opera不支持
 4、<audio src="" type="audio/mp3" />
    问题:html5标签 仅Chrome支持
 5、<audio autoplay>
       <source src="" type="audio/mp3" />
       <embed src="" type="audio/mp3"/>
    </audio>
    问题:IE6,IE7不支持,其余浏览器均支持,Opera不能自动播放
  6、<embed src=""><noembed><bgsound src=""></noembed>
    问题:IE6,IE7均不支持,其余浏览器均支持,Opera不能自动播放

  综合以上本人采取了一下方式(jquery下执行):
   if(navigator.userAgent.indexOf("Chrome") > -1){
     如果是Chrome:
     <audio src="" type="audio/mp3" autoplay=”autoplay” hidden="true"></audio>
    }else if(navigator.userAgent.indexOf("Firefox")!=-1){
     如果是Firefox:
     <embed src="" type="audio/mp3" hidden="true" loop="false" mastersound></embed>
    }else if(navigator.appName.indexOf("Microsoft Internet Explorer")!=-1 && document.all){
      如果是IE(6,7,8):
      <object classid="clsid:22D6F312-B0F6-11D0-94AB-0080C74C7E95"><param name="AutoStart" value="1" /><param name="Src" value="" /></object>
    }else if(navigator.appName.indexOf("Opera")!=-1){
       如果是Oprea:
       <embed src="" type="audio/mpeg"   loop="false"></embed>
    }else{
       <embed src="" type="audio/mp3" hidden="true" loop="false" mastersound></embed>
    }

  或
  var ua = navigator.userAgent.toLowerCase();
                    if(ua.match(/msie ([d.]+)/)){
                          jQuery('#__alert_sound').html('<object classid="clsid:22D6F312-B0F6-11D0-94AB-0080C74C7E95"><param name="AutoStart" value="1" /><param name="Src" value="/sounds/alert/1.mp3" /></object>');
                        }
                    else if(ua.match(/firefox/([d.]+)/)){
                          jQuery('#__alert_sound').html('<embed src="/sounds/alert/1.mp3" type="audio/mp3" hidden="true" loop="false" mastersound></embed>');
                        }
                    else if(ua.match(/chrome/([d.]+)/)){
                          jQuery('#__alert_sound').html('<audio src="/sounds/alert/1.mp3" type="audio/mp3" autoplay=”autoplay” hidden="true"></audio>');
                        }
                    else if(ua.match(/opera.([d.]+)/)){
                          jQuery('#__alert_sound').html('<embed src="/sounds/alert/1.mp3" hidden="true" loop="false"><noembed><bgsounds src="/sounds/alert/1.mp3"></noembed>');
                        }            
                    else if(ua.match(/version/([d.]+).*safari/)){
                          jQuery('#__alert_sound').html('<audio src="/sounds/alert/1.mp3" type="audio/mp3" autoplay=”autoplay” hidden="true"></audio>');
                        }
                    else {
                         jQuery('#__alert_sound').html('<embed src="/sounds/alert/1.mp3" type="audio/mp3" hidden="true" loop="false" mastersound></embed>');
                        }    
千里之行,始于足下
原文地址:https://www.cnblogs.com/ajieyingqulvxing/p/4661635.html