Show offline on video player
This commit is contained in:
@ -1,9 +1,15 @@
|
|||||||
import Hls from "hls.js";
|
import Hls from "hls.js";
|
||||||
import { HTMLProps, useEffect, useMemo, useRef } from "react";
|
import { HTMLProps, useEffect, useMemo, useRef, useState } from "react";
|
||||||
|
|
||||||
|
export enum VideoStatus {
|
||||||
|
Online = "online",
|
||||||
|
Offline = "offline"
|
||||||
|
}
|
||||||
|
|
||||||
export function LiveVideoPlayer(props: HTMLProps<HTMLVideoElement> & { stream?: string }) {
|
export function LiveVideoPlayer(props: HTMLProps<HTMLVideoElement> & { stream?: string }) {
|
||||||
const video = useRef<HTMLVideoElement>(null);
|
const video = useRef<HTMLVideoElement>(null);
|
||||||
const streamCached = useMemo(() => props.stream, [props.stream]);
|
const streamCached = useMemo(() => props.stream, [props.stream]);
|
||||||
|
const [status, setStatus] = useState<VideoStatus>();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (streamCached && video.current && !video.current.src && Hls.isSupported()) {
|
if (streamCached && video.current && !video.current.src && Hls.isSupported()) {
|
||||||
@ -13,17 +19,22 @@ export function LiveVideoPlayer(props: HTMLProps<HTMLVideoElement> & { stream?:
|
|||||||
hls.on(Hls.Events.ERROR, (event, data) => {
|
hls.on(Hls.Events.ERROR, (event, data) => {
|
||||||
console.debug(event, data);
|
console.debug(event, data);
|
||||||
const errorType = data.type;
|
const errorType = data.type;
|
||||||
if(errorType === Hls.ErrorTypes.NETWORK_ERROR) {
|
if (errorType === Hls.ErrorTypes.NETWORK_ERROR) {
|
||||||
hls.stopLoad();
|
hls.stopLoad();
|
||||||
hls.detachMedia();
|
hls.detachMedia();
|
||||||
|
setStatus(VideoStatus.Offline);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
hls.on(Hls.Events.MANIFEST_PARSED, () => {
|
||||||
|
setStatus(VideoStatus.Online);
|
||||||
|
})
|
||||||
return () => hls.destroy();
|
return () => hls.destroy();
|
||||||
}
|
}
|
||||||
}, [video, streamCached]);
|
}, [video, streamCached]);
|
||||||
return (
|
return (
|
||||||
<div>
|
<div className={status}>
|
||||||
<video ref={video} {...props} controls={true} />
|
<div>{status}</div>
|
||||||
|
<video ref={video} {...props} controls={status === VideoStatus.Online} />
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -63,4 +63,23 @@
|
|||||||
display: flex;
|
display: flex;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
gap: 12px;
|
gap: 12px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.live-page .offline {
|
||||||
|
background: rgba(0, 0, 0, 0.5);
|
||||||
|
display: flex;
|
||||||
|
justify-content: center;
|
||||||
|
align-items: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
.live-page .offline>div {
|
||||||
|
position: fixed;
|
||||||
|
text-transform: uppercase;
|
||||||
|
font-size: 30px;
|
||||||
|
font-weight: 700;
|
||||||
|
}
|
||||||
|
|
||||||
|
.live-page .offline>video {
|
||||||
|
z-index: -1;
|
||||||
|
position: relative;
|
||||||
}
|
}
|
Reference in New Issue
Block a user