movie-web/src/components/VideoElement.js

54 lines
1.7 KiB
JavaScript
Raw Normal View History

2021-07-14 00:31:37 +02:00
import React from 'react'
import Hls from 'hls.js'
2021-07-15 18:07:40 +02:00
import { VideoPlaceholder } from './VideoPlaceholder'
2021-07-14 00:31:37 +02:00
2021-07-20 19:28:21 +02:00
import './VideoElement.css'
2021-07-14 00:31:37 +02:00
// streamUrl: string
2021-07-15 00:09:42 +02:00
// loading: boolean
2021-07-15 20:34:25 +02:00
export function VideoElement({ streamUrl, loading, setProgress }) {
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(() => {
if (!streamUrl.endsWith('.mp4')) {
setError(false)
if (!videoRef || !videoRef.current || !streamUrl || streamUrl.length === 0 || loading) return;
const hls = new Hls();
if (!Hls.isSupported() && videoRef.current.canPlayType('application/vnd.apple.mpegurl')) {
videoRef.current.src = streamUrl;
return;
} else if (!Hls.isSupported()) {
setError(true)
return;
}
hls.attachMedia(videoRef.current);
hls.loadSource(streamUrl);
2021-07-14 00:31:37 +02:00
}
2021-07-15 00:09:42 +02:00
}, [videoRef, streamUrl, loading])
2021-07-14 01:17:34 +02:00
if (error)
return (<VideoPlaceholder>Your browser is not supported</VideoPlaceholder>)
2021-07-14 01:17:34 +02:00
2021-07-15 00:09:42 +02:00
if (loading)
2021-07-15 18:21:42 +02:00
return <VideoPlaceholder>Loading episode...</VideoPlaceholder>
2021-07-15 00:09:42 +02:00
if (!streamUrl || streamUrl.length === 0)
2021-07-15 18:41:51 +02:00
return <VideoPlaceholder>No video selected</VideoPlaceholder>
2021-07-15 00:09:42 +02:00
if (!streamUrl.endsWith('.mp4')) {
return (
<video className="videoElement" ref={videoRef} controls autoPlay onProgress={setProgress} />
)
} else {
return (
2021-07-20 19:28:21 +02:00
<video className="videoElement" ref={videoRef} controls autoPlay onProgress={setProgress}>
<source src={streamUrl} type="video/mp4" />
</video>
)
}
2021-07-14 00:31:37 +02:00
}