From 2c151916ff4431a495688b215390de50199e18fb Mon Sep 17 00:00:00 2001 From: 2betop <2betop.cn@gmail.com> Date: Fri, 21 Aug 2020 12:11:32 +0800 Subject: [PATCH] =?UTF-8?q?hls=20=E4=B9=9F=E6=B7=BB=E5=8A=A0=E6=83=B3?= =?UTF-8?q?=E5=85=B3=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/renderers/Video.tsx | 75 ++++++++++++++++++++++++++++------------- 1 file changed, 52 insertions(+), 23 deletions(-) diff --git a/src/renderers/Video.tsx b/src/renderers/Video.tsx index c22546d0..4c83779b 100644 --- a/src/renderers/Video.tsx +++ b/src/renderers/Video.tsx @@ -111,8 +111,7 @@ export class FlvSource extends React.Component { componentWillUnmount() { if (this.flvPlayer) { - this.flvPlayer.unload(); - this.flvPlayer.detachMediaElement(); + this.flvPlayer.destroy(); this.props.setError?.(''); } } @@ -201,9 +200,46 @@ export interface HlsSourceProps { } export class HlsSource extends React.Component { hls: any; + loaded = false; + unsubscribe: any; componentDidMount() { let {src, video, config, manager, isLive, autoPlay, actions} = this.props; + this.initHls({ + video, + manager, + src, + autoPlay, + actions + }); + } + componentWillUnmount() { + if (this.hls) { + this.hls.stopLoad(); + this.hls.detachMedia(); + } + } + + componentDidUpdate(prevProps: FlvSourceProps) { + const props = this.props; + let {autoPlay, actions, src, isLive, config, video, manager} = props; + + if (src !== prevProps.src) { + this.hls?.stopLoad(); + this.hls?.detachMedia(); + this.unsubscribe?.(); + this.loaded = false; + this.initHls({ + video, + manager, + src, + autoPlay, + actions + }); + } + } + + initHls({video, manager, src, autoPlay, actions}: any) { (require as any)(['hls.js'], (Hls: any) => { // load hls video source base on hls.js if (Hls.isSupported()) { @@ -215,37 +251,30 @@ export class HlsSource extends React.Component { hls.attachMedia(video); hls.loadSource(src); - let loaded = false; + this.unsubscribe = manager.subscribeToOperationStateChange( + (operation: any) => { + const type = operation.operation.action; - manager.subscribeToOperationStateChange((operation: any) => { - const type = operation.operation.action; + if (type === 'play') { + if (!this.loaded) { + this.loaded = true; + hls.startLoad(); + } - if (type === 'play') { - if (!loaded) { - loaded = true; - hls.startLoad(); + video.play(); + } else if (type === 'pause') { + video.pause(); + hls.stopLoad(); + this.loaded = false; } - - video.play(); - } else if (type === 'pause') { - video.pause(); - hls.stopLoad(); - loaded = false; } - }); + ); autoPlay && setTimeout(actions.play, 200); } }); } - componentWillUnmount() { - if (this.hls) { - this.hls.stopLoad(); - this.hls.detachMedia(); - } - } - render() { return (