2013-07-22 28 views
1

我试图在Opera 12.16(最新版本)和I noticed it doesn't work with mp3 files中播放audio元素,但它与ogg一起播放。Opera中的声音提示

这里是我的小提琴目前只适用于MP3文件和歌剧不工作: http://jsfiddle.net/hMnsd/1/

的解决方案似乎做这样的事情(如指向w3c website):

<audio controls> 
    <source src="horse.ogg" type="audio/ogg"> 
    <source src="horse.mp3" type="audio/mpeg"> 
    Your browser does not support the audio tag. 
</audio> 

我使用现在这个功能产生声音:

function notificationSound(){ 
    var audioElement = document.createElement('audio'); 
    audioElement.setAttribute('src', "http://"+ document.domain +"/files/notification2.mp3"); 
    audioElement.setAttribute('type', 'audio/mpeg'); 
    audioElement.setAttribute('autoplay', 'autoplay'); 
    //audioElement.load() 

    audioElement.addEventListener("load", function() { 
     audioElement.play(); 
    }, true); 

} 

为了增加ogg f ILE我试着不产生所需的结构成功:

var audioElement = document.createElement('audio'); 
var audioSource1 = document.createElement('source'); 
var audioSource2 = document.createElement('source'); 

//mp3 file 
audioSource1.setAttribute('src', "http://"+ document.domain +"/files/notification2.mp3"); 
audioSource1.setAttribute('type', 'audio/mpeg'); 

//ogg file 
audioSource2.setAttribute('src', "http://"+ document.domain +"/files/notification2.ogg"); 
audioSource2.setAttribute('type', 'audio/ogg'); 

audioElement.setAttribute('autoplay', 'autoplay'); 

audioElement.append(audioSource2); //opera 
audioElement.append(audioSource1); 

//audioElement.load() 
$.get(); 
audioElement.addEventListener("load", function() { 
    audioElement.get(0).play(); 
}, true); 

怎么能做到它,使其也工作在Opera与ogg文件?

回答

1

使用功能检测,而非浏览器检测:

function isMpeg() 
{ 
    var a = document.createElement('audio'); 
    return !!(a.canPlayType && a.canPlayType('audio/mpeg;').replace(/no/, '')); 
} 


function isOgg() 
{ 
    var a = document.createElement('audio'); 
    return !!(a.canPlayType && a.canPlayType('audio/ogg; codecs="vorbis"').replace(/no/, '')); 
} 

function isAAC() 
{ 
    var a = document.createElement('audio'); 
return !!(a.canPlayType && a.canPlayType('audio/mp4; codecs="mp4a.40.2"').replace(/no/, '')); 
} 

alert(isMpeg()); 
alert(isOgg()); 
alert(isAAC()); 

例子:http://jsfiddle.net/Cy9AT/1/

另见:http://diveintohtml5.info/everything.html

或使用Modernizer

1

我会检查哪个浏览器是用户使用,然后选择来源。

var audioElement = document.createElement('audio'); 
if (navigator.userAgent.search("MSIE") >= 0) { 
     audioSource1.setAttribute('src', "http://" + document.domain + "/files/notification2.mp3"); 
     audioSource1.setAttribute('type', 'audio/mpeg'); 
} else if (navigator.userAgent.search("Chrome") >= 0) { 
    audioSource2.setAttribute('src', "http://" + document.domain + "/files/notification2.ogg"); 
    audioSource2.setAttribute('type', 'audio/ogg'); 
} else if (navigator.userAgent.search("Firefox") >= 0) { 
    audioSource2.setAttribute('src', "http://" + document.domain + "/files/notification2.ogg"); 
    audioSource2.setAttribute('type', 'audio/ogg'); 
} else if (navigator.userAgent.search("Safari") >= 0 && navigator.userAgent.search("Chrome") < 0) { 
    audioSource1.setAttribute('src', "http://" + document.domain + "/files/notification2.mp3"); 
    audioSource1.setAttribute('type', 'audio/mpeg'); 
} else if (navigator.userAgent.search("Opera") >= 0) { 
    audioSource2.setAttribute('src', "http://" + document.domain + "/files/notification2.ogg"); 
    audioSource2.setAttribute('type', 'audio/ogg'); 
} 
audioElement.setAttribute('autoplay', 'autoplay'); 
audioElement.addEventListener("load", function() { 
    audioElement.play(); 
}, true); 

我希望我能正确理解你的问题。