Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if new Audio is supported by browser?

In safari 5, new Audio is not supported, so the error console displays :

TypeError : 'undefined' is not a constructor (evaluating 'new Audio')

How could I programatically know whether new Audio is supported by browser ?

like image 374
totoaussi Avatar asked May 10 '15 13:05

totoaussi


People also ask

Which audio formats are supported by the browser?

There are three supported audio formats in HTML: MP3, WAV, and OGG.

Do all browsers support MP3?

Note: Nearly all browsers support MP3 — for more details see this page on media format browser compatibility.

What will be displayed if browser doesn't support audio tag?

If the browser doesn't support the audio element, then it will display the message, “Sorry, but your browser doesn't support audio.”

Is audio supported in HTML5?

HTML5 features include native audio and video support without the need for Flash.


1 Answers

I guess you might just try it...

var createAudio = function() {
 try {
    return new Audio(); 
 } catch(e) {
    return false;
 }
};
var audio = createAudio();
if(audio) {
  // start playing... or check formats etc.
}

In case there is exception, the Audio class does not exist and returns false.

For more detailed solution check Modernzr library: http://modernizr.com/docs/#audio

The related solution is here Detecting html5 audio support with Modernizr

like image 194
Tero Tolonen Avatar answered Sep 18 '22 07:09

Tero Tolonen