audio.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /*!
  2. {
  3. "name": "HTML5 Audio Element",
  4. "property": "audio",
  5. "tags": ["html5", "audio", "media"]
  6. }
  7. !*/
  8. /* DOC
  9. Detects the audio element
  10. */
  11. define(['Modernizr', 'createElement'], function(Modernizr, createElement) {
  12. // This tests evaluates support of the audio element, as well as
  13. // testing what types of content it supports.
  14. //
  15. // We're using the Boolean constructor here, so that we can extend the value
  16. // e.g. Modernizr.audio // true
  17. // Modernizr.audio.ogg // 'probably'
  18. //
  19. // Codec values from : github.com/NielsLeenheer/html5test/blob/9106a8/index.html#L845
  20. // thx to NielsLeenheer and zcorpan
  21. // Note: in some older browsers, "no" was a return value instead of empty string.
  22. // It was live in FF3.5.0 and 3.5.1, but fixed in 3.5.2
  23. // It was also live in Safari 4.0.0 - 4.0.4, but fixed in 4.0.5
  24. Modernizr.addTest('audio', function() {
  25. var elem = createElement('audio');
  26. var bool = false;
  27. try {
  28. bool = !!elem.canPlayType;
  29. if (bool) {
  30. bool = new Boolean(bool);
  31. bool.ogg = elem.canPlayType('audio/ogg; codecs="vorbis"') .replace(/^no$/, '');
  32. bool.mp3 = elem.canPlayType('audio/mpeg; codecs="mp3"') .replace(/^no$/, '');
  33. bool.opus = elem.canPlayType('audio/ogg; codecs="opus"') ||
  34. elem.canPlayType('audio/webm; codecs="opus"') .replace(/^no$/, '');
  35. // Mimetypes accepted:
  36. // developer.mozilla.org/En/Media_formats_supported_by_the_audio_and_video_elements
  37. // bit.ly/iphoneoscodecs
  38. bool.wav = elem.canPlayType('audio/wav; codecs="1"') .replace(/^no$/, '');
  39. bool.m4a = (elem.canPlayType('audio/x-m4a;') ||
  40. elem.canPlayType('audio/aac;')) .replace(/^no$/, '');
  41. }
  42. } catch (e) {}
  43. return bool;
  44. });
  45. });