VideoJS 是一个功能齐全的 HLS 播放器,运行良好,并且
- 适用于 iOS Safari
- 支持
playsInline prop 避免在 iOS 移动设备上全屏
注意:你也可以在 iOS 上使用自动播放,只要视频开始静音
首先,您需要在主 HTML 中添加对 videojs 和 HLS 插件的依赖项,如 in the docs of videojs HLS plugin 解释的那样
然后,您可以使用如下所示的 react 包装器,根据自己的喜好进行修改:
import React, { PropTypes } from 'react';
export default class VideoPlayer extends Component {
static propTypes = {
style: PropTypes.object,
onVideoEvent: PropTypes.func,
src: PropTypes.string,
poster: PropTypes.string
}
static defaultProps = {
style: {},
onVideoEvent: console.log,
src: '',
poster: ''
}
componentDidMount = () => {
// This is a hack because I don't import video.js as a hard dependency
// but load it alongside my app bundle
if (typeof videojs === 'undefined') {
setTimeout(this.componentDidMount, 500);
return;
}
const { onVideoEvent } = this.props;
this.player = videojs(this.videoNode);
const exportEvents = ['timeupdate', 'play', 'playing', 'pause',
'ended', 'error', 'waiting'];
exportEvents.forEach(ev => this.player.on(ev, this.props.onVideoEvent));
}
componentWillUnmount = () => {
this.player && this.player.dispose();
this.player = null;
}
render = () => (
<div alt="snap"
key="media"
style={ this.props.style }
data-vjs-player>
<video playsInline
className="video-js"
preload="auto"
poster={ this.props.poster }
ref={ r => { this.videoNode = r; } } >
<source src={ this.props.src } type="application/x-mpegURL" />
</video>
</div>
)
}
Video.js 的完整选项和文档以及播放器的所有自定义和功能can be found here