Blame view

src/components/flvCom/flvCom.js 2.07 KB
f248ca05   Zhang Zhuo   初始化版本 -张卓
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
  import React, { Component } from "react"
  import flvjs from "flv.js";
  import "./flvCom.css"
  export default class FlvCom extends Component {
      constructor(props){
          super(props);
          this.state = {
              vidoeUrl:"",
              id:new Date().getTime()+Math.floor(Math.random() * (10000 - 1)) + 1,
          }
      }
      static getDerivedStateFromProps(props){
          let {vidoeUrl} = props
          if(vidoeUrl){
              return{
                  vidoeUrl
              }
          }
          return null;
      }
      componentDidUpdate(){
          if(this.state.vidoeUrl){
              this.startFlv()
          }
      }
      componentDidMount(){
          if(this.state.vidoeUrl){
              this.startFlv()
          }
      }
      startFlv(){
          let {vidoeUrl,id} = this.state
          if (flvjs.isSupported()) {
              const videoElement = document.getElementById(id);
              if(this.flvPlayer) this.destroyFlv()
              this.flvPlayer = flvjs.createPlayer(
                {
                  type: "flv",
                  isLive: true,
                  hasAudio: false,
                  //必须与node搭建的流媒体服务器中的http的端口和推流的参数吻合
                  url: vidoeUrl,
                },
                {
                  enableStashBuffer: true,
                  stashInitialSize: 128,
                }
              );
              this.flvPlayer.attachMediaElement(videoElement);
              this.flvPlayer.load();
              this.flvPlayer.play();
            } else {
                alert("请更换浏览器,该浏览器暂不支持。")
            }
      }
      destroyFlv(){
          this.flvPlayer.pause();
          this.flvPlayer.unload();
          this.flvPlayer.detachMediaElement();
          this.flvPlayer.destroy();
          this.flvPlayer = null;
      }
      render() {
          return (
              <div className="flvWai">
                  <video
                      id={this.state.id}
                      className="videoFlv"
                      autoPlay
                      controls
                      muted
                  ></video>
              </div>
          )
      }
  }