2021-07-13 22:31:37 +00:00
|
|
|
import React from 'react'
|
|
|
|
import Hls from 'hls.js'
|
2021-07-15 16:07:40 +00:00
|
|
|
import { VideoPlaceholder } from './VideoPlaceholder'
|
2021-07-13 22:31:37 +00:00
|
|
|
|
2021-07-20 17:28:21 +00:00
|
|
|
import './VideoElement.css'
|
|
|
|
|
2021-07-13 22:31:37 +00:00
|
|
|
// streamUrl: string
|
2021-07-14 22:09:42 +00:00
|
|
|
// loading: boolean
|
2021-08-07 20:19:01 +00:00
|
|
|
// setProgress: (event: NativeEvent) => void
|
|
|
|
// videoRef: useRef
|
|
|
|
// startTime: number
|
2021-09-13 18:27:14 +00:00
|
|
|
export function VideoElement({ streamUrl, loading, setProgress, videoRef, startTime, streamData }) {
|
2021-07-13 23:17:34 +00:00
|
|
|
const [error, setError] = React.useState(false);
|
2021-07-13 22:31:37 +00:00
|
|
|
|
2021-08-07 20:19:01 +00:00
|
|
|
function onLoad() {
|
|
|
|
if (startTime)
|
|
|
|
videoRef.current.currentTime = startTime;
|
|
|
|
}
|
|
|
|
|
2021-07-13 22:31:37 +00:00
|
|
|
React.useEffect(() => {
|
2021-12-30 19:23:15 +00:00
|
|
|
if (!streamUrl.includes('.mp4')) {
|
2021-07-19 21:26:49 +00:00
|
|
|
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-13 22:31:37 +00:00
|
|
|
}
|
2021-08-07 19:27:22 +00:00
|
|
|
}, [videoRef, streamUrl, loading]);
|
|
|
|
|
2021-07-13 23:17:34 +00:00
|
|
|
if (error)
|
2021-07-15 16:28:41 +00:00
|
|
|
return (<VideoPlaceholder>Your browser is not supported</VideoPlaceholder>)
|
2021-07-13 23:17:34 +00:00
|
|
|
|
2021-07-14 22:09:42 +00:00
|
|
|
if (loading)
|
2021-07-15 16:21:42 +00:00
|
|
|
return <VideoPlaceholder>Loading episode...</VideoPlaceholder>
|
2021-07-14 22:09:42 +00:00
|
|
|
|
|
|
|
if (!streamUrl || streamUrl.length === 0)
|
2021-07-15 16:41:51 +00:00
|
|
|
return <VideoPlaceholder>No video selected</VideoPlaceholder>
|
2021-07-14 22:09:42 +00:00
|
|
|
|
2021-12-30 19:23:15 +00:00
|
|
|
if (!streamUrl.includes('.mp4')) {
|
2021-07-19 21:26:49 +00:00
|
|
|
return (
|
2022-01-31 00:39:28 +00:00
|
|
|
<video className="videoElement" ref={videoRef} controls autoPlay onProgress={setProgress} onLoadedData={onLoad}>
|
|
|
|
{ streamData.subtitles && streamData.subtitles.map((sub, index) => <track key={index} kind="captions" label={sub.language} src={sub.file} />) }
|
2021-09-13 18:27:14 +00:00
|
|
|
</video>
|
2021-07-19 21:26:49 +00:00
|
|
|
)
|
|
|
|
} else {
|
|
|
|
return (
|
2022-01-31 00:39:28 +00:00
|
|
|
<video className="videoElement" ref={videoRef} controls autoPlay onProgress={setProgress} onLoadedData={onLoad}>
|
|
|
|
{ streamData.subtitles && streamData.subtitles.map((sub, index) => <track key={index} kind="captions" label={sub.language} src={sub.file} />) }
|
2021-07-20 17:28:21 +00:00
|
|
|
<source src={streamUrl} type="video/mp4" />
|
|
|
|
</video>
|
2021-07-19 21:26:49 +00:00
|
|
|
)
|
|
|
|
}
|
2021-07-13 22:31:37 +00:00
|
|
|
}
|