我試圖在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
文件?