movie-web/src/components/VideoElement.js

44 lines
1.3 KiB
JavaScript
Raw Normal View History

2021-07-14 00:31:37 +02:00
import React from 'react'
import Hls from 'hls.js'
import './VideoElement.css'
// streamUrl: string
2021-07-15 00:09:42 +02:00
// loading: boolean
export function VideoElement({ streamUrl, loading }) {
2021-07-14 00:31:37 +02:00
const videoRef = React.useRef(null);
2021-07-14 01:17:34 +02:00
const [error, setError] = React.useState(false);
2021-07-14 00:31:37 +02:00
React.useEffect(() => {
2021-07-14 01:17:34 +02:00
setError(false)
2021-07-15 00:09:42 +02:00
if (!videoRef || !videoRef.current || !streamUrl || streamUrl.length === 0 || loading) return;
2021-07-14 00:31:37 +02:00
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-14 01:17:34 +02:00
setError(true)
return;
2021-07-14 00:31:37 +02:00
}
hls.attachMedia(videoRef.current);
hls.loadSource(streamUrl);
2021-07-15 00:09:42 +02:00
}, [videoRef, streamUrl, loading])
// TODO make better loading/error/empty state
2021-07-14 00:31:37 +02:00
2021-07-14 01:17:34 +02:00
if (error)
return (<p className="videoElementText">Your browser is not supported</p>)
2021-07-15 00:09:42 +02:00
if (loading)
return <p className="videoElementText">Loading episode</p>
if (!streamUrl || streamUrl.length === 0)
return <p className="videoElementText">No video selected</p>
2021-07-14 00:31:37 +02:00
return (
<video className="videoElement" ref={videoRef} controls autoPlay />
)
}