2013-07-22 27 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); 

我希望我能正確理解你的問題。