在html中插入音頻文件在瀏覽器中播放音頻文件的兼容性問題

字號:


    下面談?wù)劚救嗽趆tml中插入音頻文件,經(jīng)過我的本地測試總結(jié)的一些問題(播放mp3文件):
    1、<embed type="audio/mp3" src="" autostart=true loop=false></embed>
    問題:IE8上正常(通過media player插件來播放)但在IE6和IE7上不會播放
    Firefox上要安裝QuickTime插件才能播放
    Chrome通過將其轉(zhuǎn)化成html5上的<vidio>標(biāo)簽播放,能播放但會使整個屏幕藍(lán)屏
    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標(biāo)簽 僅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下執(zhí)行):
    代碼如下:
    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>');
    }