美文网首页
react videojs播放RTMP视频流(flash)

react videojs播放RTMP视频流(flash)

作者: Sasoli | 来源:发表于2019-06-26 11:00 被阅读0次

首先在项目中安装videojs插件是肯定的

npm install video.js

然后可以按照官方文档搭建两个组件,一个是video播放器组件,一个是页面组件
video播放器组件我这里放在components文件夹下,以下代码从官方文档拷贝

import React from 'react';
import videojs from 'video.js'

export default class VideoPlayer extends React.Component {
  componentDidMount() {
    // instantiate Video.js
    this.player = videojs(this.videoNode, this.props, function onPlayerReady() {
      console.log('onPlayerReady', this)
    });
  }

  // destroy player on unmount
  componentWillUnmount() {
    if (this.player) {
      this.player.dispose()
    }
  }

  // wrap the player in a div with a `data-vjs-player` attribute
  // so videojs won't create additional wrapper in the DOM
  // see https://github.com/videojs/video.js/pull/3856
  render() {
    return (
      <div> 
        <div data-vjs-player>
          <video ref={ node => this.videoNode = node } className="video-js"></video>
        </div>
      </div>
    )
  }
}

然后有几个地方需要改动

import React from 'react';
import videojs from 'video.js'
import videozhCN from 'video.js/dist/lang/zh-CN.json'; //播放器中文,不能使用.js文件
import 'video.js/dist/video-js.css';  //样式文件注意要加上
import 'videojs-flash';  //如果要播放RTMP要使用flash 需要先npm i videojs-flash

export default class VideoPlayer extends React.Component {
  componentDidMount() {
    // instantiate Video.js
    //这里的this.props是上级传进来的video的options
    this.player = videojs(this.videoNode, this.props, function onPlayerReady() {
      console.log('onPlayerReady', this)
    });
    videojs.addLanguage('zh-CN', videozhCN);

    // this.player.liveTracker.on('liveedgechange', () => {
      // console.log('跟随直播');
      // this.player.liveTracker.seekToLiveEdge();
    // });
  }

  // destroy player on unmount
  componentWillUnmount() {
    if (this.player) {
      this.player.dispose()
    }
  }

  // wrap the player in a div with a `data-vjs-player` attribute
  // so videojs won't create additional wrapper in the DOM
  // see https://github.com/videojs/video.js/pull/3856
  render() {
    return (
      <div> 
        <div data-vjs-player>  {/*这个带有属性的div目前没看到作用,可以去掉*/}
          <video ref={ node => this.videoNode = node } className="video-js"></video>
        </div>
      </div>
    )
  }
}

最后是父组件,这里不粘贴完整代码了,把相关的写下

import VideoPalyer from '../../components/VideoPlayer';  //先引入子组件

render(){
  const videoJsOptions = {
      autoplay: true,  //自动播放
      language: 'zh-CN', 
      controls: true,  //控制条
      preload: 'auto',  //自动加载
      errorDisplay: true,  //错误展示
      width: 500,  //宽
      height: 300,  //高
      // fluid: true,  //跟随外层容器变化大小,跟随的是外层宽度
      // controlBar: false,  // 设为false不渲染控制条DOM元素,只设置controls为false虽然不展示,但还是存在
      // textTrackDisplay: false,  // 不渲染字幕相关DOM
      userActions: {
        hotkeys: true  //是否支持热键
      },
      sources: [
        {
           src: 'rtmp://live.hkstv.hk.lxdns.com/live/hks1',
           type: "rtmp/flv",  //类型可加可不加,目前未看到影响
          // type: 'video/mp4',
        }
      ]
  };
  return(
    <div>
      <VideoPlayer {...videoJsOptions} />
    </div>
  )
}

官方文档:https://docs.videojs.com/tutorial-react.html

相关文章

网友评论

      本文标题:react videojs播放RTMP视频流(flash)

      本文链接:https://www.haomeiwen.com/subject/wdmxcctx.html