6

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 ?

3

2 Answers 2

3

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

0
2

There are multiple solutions to check if it exists.

Check that the type of Audio isn't "undefined".

if (typeof window.Audio !== "undefined")

Check if window has a property Audio.

if ("Audio" in window)

The below could generate an error.

if (window.Audio)

But, this code doesn't guarantee that it's a good Audio implementation. It could just be some random script doing this: window.Audio = 'http://link.to/some/mp3'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.