Skip to content Skip to sidebar Skip to footer

In Search For A Library That Knows To Detect Support For Specific Audio Video Format Files

I`m building a mobile-web app, and I am implementing Video & Audio tags. Apparently not all devices know to run all file formats. Modernizr knows to return to me the Codec, but

Solution 1:

If you have access to the mime type you could simply use the audio or video's canPlayType() method:

canPlay = (function(){
  var a = document.createElement('audio'),
      v = document.createElement('video');

  functioncanPlayAudio(type){
    var mime = 'audio/';
    if (a && a.canPlayType) {
      if (type.indexOf(mime) === -1) type = mime+type;
      return !!a.canPlayType(type) || !!a.canPlayType(type.replace(/^audio\/(x-)?(.+)$/, mime+'x-$2'))
    } returnfalse
  }

  functioncanPlayVideo(type){
    var mime = 'video/';
    if (v && v.canPlayType) {
      if (type.indexOf(mime) === -1) type = mime+type;
      return !!v.canPlayType(type) || !!v.canPlayType(type.replace(/^video\/(x-)?(.+)$/, mime+'x-$2'))
    } returnfalse
  }

  return {
    audio: canPlayAudio,
    video: canPlayVideo
  }
})();

Then you can perform the tests like this (note: including the "audio/" or "video/" part is optional):

canPlay.audio('audio/mpeg')  // true
canPlay.audio('m4a')         // true
canPlay.audio('wav')         // true
canPlay.audio('flac')        // false
canPlay.audio('audio/ogg')   // true

canPlay.video('video/mpeg')  // false
canPlay.video('video/mp4')   // true
canPlay.video('m4v')         // true
canPlay.video('video/webm')  // true
canPlay.video('avi')         // false

Post a Comment for "In Search For A Library That Knows To Detect Support For Specific Audio Video Format Files"