allVideoCom.js 3.38 KB
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>
        )
    }
}