6
votes

I'm using html5 audio tag and can't seem to find what to specify for the codec for both mp3 and wav.

I know ogg is:

<audio>
<source type='audio/ogg; codec="vorbis"' />
</audio>

Anyone know what I would write for mp3 and wav?

3

3 Answers

5
votes

The best reference for <source type=""> keywords is not the IANA media types registry, as you might think, but developer.mozilla.org's "Media formats for HTML audio and video" article, which documents what browsers (not just Firefox) actually implement, instead of what the RFCs say is supposed to happen. (For instance, there is no official MIME type for .WAV files.)

For the audio container formats you mention, these are my recommended source tags:

  • Ogg: <source type="audio/ogg">
  • MP3: <source type="audio/mpeg">
  • WAV: <source type="audio/wav"> (annoyingly, audio/wave is simultaneously "preferred" and "does not work with Chrome", thanks ever so much Google).

I recommend you do not specify exactly which codec you are using, because this embeds details that could change into the HTML. The browser will figure it out. (Really, this entire mess should have been handled using a generic URL directly on the audio tag, and an Accept: header on the HTTP request, but nobody listens to me.)

1
votes

what browser are you using for playing the mp3 file? not all browsers support playing of mp3 trough the audio tag yet. see: http://dev.opera.com/articles/view/html5-audio-radio-player/ I suggest making a script that chooses the audio type according to the browser type. That way the user always has playing music.

0
votes

Your syntax for Vorbis in Ogg is close to correct, just be sure to use codecs, plural:

audio/ogg; codecs=vorbis

Browsers do not support PCM and MP3 in Ogg. But, PCM is supported in WAV, and for that you would use:

audio/wav; codecs=1

(For the constant 1, WAVE_FORMAT_PCM, see the Appendix A of RFC2361.)

For normal MPEG Layer 3 audio, there is no real container other than the bitstream itself, so just use:

audio/mpeg

Finally, as others have mentioned, you don't need to explicitly specify the codecs. Specifying the file type is enough. But, if you do already know the codecs it helps to specify, for efficient loading and less probing.