dB'
dB'

Reputation: 8330

Supported audio file formats for Chrome?

I'm interested in using the Web Audio API. Unfortunately my audio files are are all in an esoteric format that Chrome can't decode. (They're .wavs, but sampled at 96 kHz with 32-bit float encoding.)

Is there any way for me to query my browser (Chrome) to find out exactly which audio formats and encodings it supports?

UPDATE

I've found a list of file formats supported by Chrome here: https://sites.google.com/a/chromium.org/dev/audio-video

Upvotes: 8

Views: 15706

Answers (4)

idbehold
idbehold

Reputation: 17168

Using Lo-Dash:

(function(){
  var a = document.createElement('audio'),
      types = _(navigator.mimeTypes).pluck('type'),
      isAudio = /^audio\//, canPlay = {};
  if (a && a.canPlayType) {
    types
      .push('audio/flac', 'audio/opus', 'audio/webm', 'audio/ogg', 'audio/midi')
      .flatten()
      .uniq()
      .each(function(type){
        if (isAudio.test(type)) {
          canPlay[type] = !!a.canPlayType(type);
        }
      });
  }
  return canPlay;
})();

Upvotes: 0

Fabien Snauwaert
Fabien Snauwaert

Reputation: 5651

A non-programmatic way would be those sites:

Upvotes: 0

Boris Smus
Boris Smus

Reputation: 8472

You could test this sort of thing by trying to load a variety of sample files using a try...catch construct, and seeing which filetypes load and which don't. See this tutorial for loading files with the Web Audio API in Chrome.

Upvotes: 4

Albert H
Albert H

Reputation: 1051

There is! I don't know how reliable this is, but...

// Need to check the canPlayType first or an exception
// will be thrown for those browsers that don't support it      

var myAudio = document.createElement('audio'); 

if (myAudio.canPlayType) {
   // Currently canPlayType(type) returns: "", "maybe" or "probably" 
   var canPlayMp3 = !!myAudio.canPlayType && "" != myAudio.canPlayType('audio/mpeg');
   var canPlayOgg = !!myAudio.canPlayType && "" != myAudio.canPlayType('audio/ogg; codecs="vorbis"');
}

Since we're talking about WAV files here, I would use these:

audio/vnd.wave, audio/wav, audio/wave, audio/x-wav

The best thing to do is to figure out what your file's MIME type is (should be one of the above), and then check for that with something like this:

var canPlayWav = !!myAudio.canPlayType && "" != myAudio.canPlayType('MIME_TYPE_HERE');
if (canPlayWav) { dothis(); } else { dothat(); }

I hope this helps!

Source: http://html5doctor.com/native-audio-in-the-browser/

Upvotes: 0

Related Questions