gargantuan
gargantuan

Reputation: 8944

Is there a javascript way to check if a browser natively supports MP3?

If the browser can play MP3's then I'll use the audio tag to play a file. If not, I'll have to load in a flash player.

Upvotes: 14

Views: 9345

Answers (2)

Eli Grey
Eli Grey

Reputation: 35905

var audio  = document.createElement("audio"),
canPlayMP3 = (typeof audio.canPlayType === "function" &&
              audio.canPlayType("audio/mpeg;codecs=mp3") !== "");

Edit:

If you don't want to use JavaScript (yes, this will work in browsers that support <audio> but not MP3), try this:

<audio controls="controls">
<source src="some-audio-file.mp3" type="audio/mpeg;codecs=mp3" />
<!-- if you have an Opus version, also include this:
<source src="some-audio-file.opus" type="audio/ogg;codecs=opus" />
-->
<!-- flash object goes here -->
</audio>

If you want auto-play, include an autoplay attribute on the audio element.

Upvotes: 18

mickael9
mickael9

Reputation: 488

Just put the code that displays the flash sound player in the tag.

Edit : Use the canPlayType function : http://www.whatwg.org/specs/web-apps/current-work/multipage/media-elements.html#dom-navigator-canplaytype
You can also look at the error attribute : http://www.whatwg.org/specs/web-apps/current-work/multipage/media-elements.html#dom-media-error

Upvotes: 4

Related Questions