hls 也添加想关逻辑

This commit is contained in:
2betop 2020-08-21 12:11:32 +08:00
parent 2877efda86
commit 7e1fa4f462
1 changed files with 52 additions and 23 deletions

View File

@ -111,8 +111,7 @@ export class FlvSource extends React.Component<FlvSourceProps, any> {
componentWillUnmount() { componentWillUnmount() {
if (this.flvPlayer) { if (this.flvPlayer) {
this.flvPlayer.unload(); this.flvPlayer.destroy();
this.flvPlayer.detachMediaElement();
this.props.setError?.(''); this.props.setError?.('');
} }
} }
@ -201,9 +200,46 @@ export interface HlsSourceProps {
} }
export class HlsSource extends React.Component<HlsSourceProps, any> { export class HlsSource extends React.Component<HlsSourceProps, any> {
hls: any; hls: any;
loaded = false;
unsubscribe: any;
componentDidMount() { componentDidMount() {
let {src, video, config, manager, isLive, autoPlay, actions} = this.props; 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) => { (require as any)(['hls.js'], (Hls: any) => {
// load hls video source base on hls.js // load hls video source base on hls.js
if (Hls.isSupported()) { if (Hls.isSupported()) {
@ -215,14 +251,13 @@ export class HlsSource extends React.Component<HlsSourceProps, any> {
hls.attachMedia(video); hls.attachMedia(video);
hls.loadSource(src); hls.loadSource(src);
let loaded = false; this.unsubscribe = manager.subscribeToOperationStateChange(
(operation: any) => {
manager.subscribeToOperationStateChange((operation: any) => {
const type = operation.operation.action; const type = operation.operation.action;
if (type === 'play') { if (type === 'play') {
if (!loaded) { if (!this.loaded) {
loaded = true; this.loaded = true;
hls.startLoad(); hls.startLoad();
} }
@ -230,22 +265,16 @@ export class HlsSource extends React.Component<HlsSourceProps, any> {
} else if (type === 'pause') { } else if (type === 'pause') {
video.pause(); video.pause();
hls.stopLoad(); hls.stopLoad();
loaded = false; this.loaded = false;
} }
}); }
);
autoPlay && setTimeout(actions.play, 200); autoPlay && setTimeout(actions.play, 200);
} }
}); });
} }
componentWillUnmount() {
if (this.hls) {
this.hls.stopLoad();
this.hls.detachMedia();
}
}
render() { render() {
return ( return (
<source <source