Search code examples
htmlhtml5-audiobrowser-feature-detection

How to detect HTML5 audio MP3 support?


I know how to check in Javascript if HTML5 audio playback is available. But how do I specifically check if MP3 audio playback is available, as IE9 and Chrome support it, while Firefox and Opera do not.


Solution

  • You could either check the User-Agent and see what browser is being used or you could test support with Javascript.

    var a = document.createElement('audio');
    return !!(a.canPlayType && a.canPlayType('audio/mpeg;').replace(/no/, ''));
    

    I got the above code from this page.

    return !!(a.canPlayType) is better because (some recent versions of)Firefox not supports mp3 and a.canPlayType('audio/mpeg;') will be false