sudo-archive/src/components/VideoElement.js

35 lines
972 B
JavaScript
Raw Normal View History

2021-07-13 22:31:37 +00:00
import React from 'react'
import Hls from 'hls.js'
import './VideoElement.css'
// streamUrl: string
export function VideoElement({ streamUrl }) {
const videoRef = React.useRef(null);
2021-07-13 23:17:34 +00:00
const [error, setError] = React.useState(false);
2021-07-13 22:31:37 +00:00
React.useEffect(() => {
2021-07-13 23:17:34 +00:00
setError(false)
2021-07-13 22:31:37 +00:00
if (!videoRef || !videoRef.current) return;
const hls = new Hls();
if (!Hls.isSupported() && videoRef.current.canPlayType('application/vnd.apple.mpegurl')) {
videoRef.current.src = streamUrl;
return;
} else if (!Hls.isSupported()) {
2021-07-13 23:17:34 +00:00
setError(true)
return;
2021-07-13 22:31:37 +00:00
}
hls.attachMedia(videoRef.current);
hls.loadSource(streamUrl);
}, [videoRef, streamUrl])
2021-07-13 23:17:34 +00:00
if (error)
return (<p className="videoElementText">Your browser is not supported</p>)
2021-07-13 22:31:37 +00:00
return (
<video className="videoElement" ref={videoRef} controls autoPlay />
)
}