Skip to content Skip to sidebar Skip to footer

Sound Notifications In Opera

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 currentl

Solution 1:

Use feature detection instead of browser detection:

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


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

functionisAAC()
{
    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

Solution 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');
 } elseif (navigator.userAgent.search("Chrome") >= 0) {
     audioSource2.setAttribute('src', "http://" + document.domain + "/files/notification2.ogg");
     audioSource2.setAttribute('type', 'audio/ogg');
 } elseif (navigator.userAgent.search("Firefox") >= 0) {
     audioSource2.setAttribute('src', "http://" + document.domain + "/files/notification2.ogg");
     audioSource2.setAttribute('type', 'audio/ogg');
 } elseif (navigator.userAgent.search("Safari") >= 0 && navigator.userAgent.search("Chrome") < 0) {
     audioSource1.setAttribute('src', "http://" + document.domain + "/files/notification2.mp3");
     audioSource1.setAttribute('type', 'audio/mpeg');
 } elseif (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.

Post a Comment for "Sound Notifications In Opera"