Blame view

src/components/allVideoCom/allVideoCom.js 3.38 KB
a031df93   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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
  import React, { Component } from "react"
  import "./allVideoCom.css"
  import flvjs from "flv.js";
  import DPlayer from 'dplayer';
  export default class AllVideoCom extends Component {
      constructor(props) {
          super(props);
          this.state = {
              id: new Date().getTime() + Math.floor(Math.random() * (10000 - 1)) + 1,
              videoUrl: ""
          }
      }
      shouldComponentUpdate(nextProps) {
          if (nextProps.videoUrl !== this.props.videoUrl || !this.state.videoUrl) {
              if (nextProps.videoUrl) {
                  this.playerFlvVideo(nextProps.videoUrl)
              }
              
              return true
          }
          return false
      }
      componentDidMount(){
          if(this.props.videoUrl){
              this.playerFlvVideo(this.props.videoUrl)
          }
      }
      playerFlvVideo(videoUrl) {
          this.setState({ videoUrl })
          if (!this.dp) {
              this.createFlvPlayer(videoUrl)
          } else {
              this.dp.destroy();
              this.flvPlayer.pause();
              this.flvPlayer.unload();
              this.flvPlayer.detachMediaElement();
              this.flvPlayer.destroy();
              this.flvPlayer = null;
              this.dp = null;
              this.createFlvPlayer(videoUrl)
          }
      }
      createFlvPlayer(videoUrl) {
          let { id } = this.state
          let { autoplay=true, config = {}, maxCount = 5 } = this.props;
          this.count = 0;//重连次数
          this.dp = new DPlayer({
              container: document.getElementById(id),
              screenshot: true,
              autoplay: autoplay,
              airplay: true,
              mutex: false,
              video: {
                  url: videoUrl,
                  type: "flv",
                  type: "customFlv",
                  customType: {
                      customFlv: (video) => {
                          this.flvPlayer = flvjs.createPlayer({
                              type: 'flv',
                              url: video.src,
                          }, Object.assign({
                              autoCleanupSourceBuffer: true
                          }, config));
                          this.flvPlayer.attachMediaElement(video);
                          this.flvPlayer.load();
                          this.flvPlayer.on("error", (e) => {
                              if (e && e === "NetworkError") {
                                  this.count++
                                  if (parseInt(maxCount) >= this.count) {
                                      this.flvPlayer.pause();
                                      this.flvPlayer.unload();
                                      this.flvPlayer.detachMediaElement();
                                      setTimeout(() => {
                                          this.flvPlayer.attachMediaElement(video);
                                          this.flvPlayer.load();
                                          this.flvPlayer.play();
                                      }, 500)
                                  } else {
                                      console.log("重连失败")
                                  }
                              }
                          })
                      },
                  },
              },
          });
      }
      render() {
          let { id } = this.state;
          return (
              <div className="dplayerWaikuang">
                  <div id={id} style={{ width: "100%", height: "100%" }}></div>
              </div>
          )
      }
  }