Sound notifications in Opera

青春壹個敷衍的年華 提交于 2019-12-24 16:35:10

问题


I am trying to play an audio element in Opera 12.16 (the latest version right now) and I noticed it doesn't work with mp3 files but it does with ogg ones.

Here's my fiddle currently only with the mp3 file and NOT working on Opera: http://jsfiddle.net/hMnsd/1/

The solution seem to do something like this (as pointed in 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>

I am using now this function to generate the sound:

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);

}

In order to add the ogg file I've tried to generate the needed structure without succeed:

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);

How could accomplish it to make it work also in Opera with ogg files?


回答1:


Use feature detection instead of browser detection:

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());

Example: http://jsfiddle.net/Cy9AT/1/

Also see: http://diveintohtml5.info/everything.html

Or Use Modernizer




回答2:


I would check which browser is user using and then select source.

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);

I hope I understand your question correctly.



来源:https://stackoverflow.com/questions/17791602/sound-notifications-in-opera

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!