Griffith 是一个基于 React 的视频播放器,目前已在知乎 web 和 mobile web 内使用,并在 GitHub 上开源。html
GitHub 地址:https://github.com/zhihu/griffithreact
CodeSandbox 示例:codesandbox.io/s/74olr5z02…webpack
Griffith 提供了简洁易用的播放器 UI。目前知乎网页端视频播放器就是使用的 Griffith。
git
Griffith 参考 YouTube 进行了快捷键支持,后续还会添加更多的快捷键。github
Griffith 是一个基于 React 开发的 web 视频播放器。对于 React 应用,能够直接经过组件调用的方式使用。web
Griffith 使用 Context API 进行状态管理。对于 React 应用,能够经过引入 Griffith 的 Context 来实现弹幕等自定义功能。npm
对于非 React 应用,或者不肯意经过 npm 包安装的用户,Griffith 提供了 standalone 模式能够免构建工具直接在浏览器中使用。浏览器
Griffith 定义了丰富的事件系统。bash
对于视频播放器中常见的首帧时长,缓冲次数等指标,能够经过接收 Griffith 事件来进行打点记录。ide
对于一些须要与播放器进行通讯的功能,能够经过往 Griffith 发送事件来与播放器进行交互。
Griffith 使用了 Media Source Extensions™ ,支持对 mp4 和 m3u8 格式的视频进行流式播放。
import Player from 'griffith'
const sources = {
hd: {
play_url: 'https://zhstatic.zhihu.com/cfe/griffith/zhihu2018_hd.mp4',
},
sd: {
play_url: 'https://zhstatic.zhihu.com/cfe/griffith/zhihu2018_sd.mp4',
},
}
render(<Player sources={sources} />)复制代码
<div id="player"></div>
<script src="https://unpkg.com/griffith-standalone/dist/index.umd.min.js"></script>
<script>
const target = document.getElementById('player')
const sources = {
hd: {
play_url: 'https://zhstatic.zhihu.com/cfe/griffith/zhihu2018_hd.mp4',
} ,
sd: {
play_url: 'https://zhstatic.zhihu.com/cfe/griffith/zhihu2018_sd.mp4',
},
}
// 建立播放器
const player = Griffith.createPlayer(target)
// 载入视频
player.render({sources})
// 销毁视频
player.dispose()
</script>复制代码
Griffith 全部的工做都会在 GitHub 上进行(知乎内部使用的也是同一个仓库)。若是有任何相关的疑问和 bug,欢迎在 GitHub 提交 issue、PR 帮助 Griffith 变得更好。