首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用JS的浏览器支持哪种音视频`编解码器`,可以查看一下吗?

使用JS的浏览器支持哪种音视频`编解码器`,可以查看一下吗?
EN

Stack Overflow用户
提问于 2016-09-08 22:36:27
回答 1查看 3.5K关注 0票数 3

更准确地说,我想检查一下可以在浏览器中使用哪些编解码器来处理HTML5中的视频/音频元素。(例如,Safari支持H.264,但我也想知道我可以使用哪些编解码器)

由于规范会随着时间的推移而变化,所以我想以某种方式自动完成它,而不是基于浏览器编解码表。

我认为,它可以被写成JS中的一个函数(如果还没有写的话)。我试着用谷歌搜索它,但还没有找到答案。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-09-08 22:40:56

参考链接:https://www.nomensa.com/blog/2011/detecting-browser-compatibility-html5-video-and-audio您可以这样做:

代码语言:javascript
复制
// Define the get_mime function
var get_mime = function(filetype) {
  var mimetype = '';
  var media_container = 'video';
  switch (filetype) {
    case 'mp4':
      mimetype = 'video/mp4; codecs="avc1.42E01E, mp4a.40.2"';
      break;
    case 'ogg':
      mimetype = 'video/ogg; codecs="theora, vorbis"';
      break;
    case 'webm':
      mimetype = 'video/webm; codecs="vp8, vorbis"';
      break;
    case 'mp3':
      mimetype = 'audio/mpeg';
      media_container = 'audio';
      break;
  }
  return {
    'mimetype': mimetype,
    'container': media_container
  };
};

// Check to see if the browser can render the file type
// using HTML5
var supports_media = function(mimetype, container) {
  var elem = document.createElement(container);
  if (typeof elem.canPlayType == ‘ function’) {
    var playable = elem.canPlayType(mimetype);
    if ((playable.toLowerCase() == 'maybe') || (playable.toLowerCase() == 'probably')) {
      return true;
    }
  }
  return false;
};

// When the DOM has loaded check the file extension of each media link
// and serve up appropriate media player
$(document).ready(function() {
  $(‘a.youtube - links’).each(function() {
    var path = $(this).attr(‘href’);
    var extension = path.substring(path.lastIndexOf('.') + 1);
    var extension_info = get_mime(extension);
    if (supports_media(extension_info.mimetype, extension_info.container)) {
      // Serve up an HTML5 Media Player and controls
      serve_html5();
    } else {
      // Serve up a flash based video for browsers that
      //will not play the file using HTML5
      serve_flash();
    }
  });
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39393784

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档