-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #66 from mykhailodanilenko/feature/web-hls-playback
Add support for HLS on web
- Loading branch information
Showing
17 changed files
with
549 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* eslint-disable react-native/no-raw-text */ | ||
import type { PropsWithChildren } from "react"; | ||
|
||
export default function Root({ children }: PropsWithChildren) { | ||
return ( | ||
<html lang="en"> | ||
<head> | ||
<meta charSet="utf-8" /> | ||
<meta httpEquiv="X-UA-Compatible" content="IE=edge" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /> | ||
<style id="expo-reset">{"html, body { height: 100%; } #root { display: flex; height: 100%; flex: 1; }"}</style> | ||
<script src="https://vjs.zencdn.net/8.10.0/video.min.js"></script> | ||
</head> | ||
<body> | ||
<noscript>You need to enable JavaScript to run this app.</noscript> | ||
{children} | ||
</body> | ||
</html> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { ActivityIndicator, StyleSheet, View } from "react-native"; | ||
|
||
export const Loader = () => { | ||
return ( | ||
<View style={styles.container}> | ||
<ActivityIndicator size="large" /> | ||
</View> | ||
); | ||
}; | ||
|
||
const styles = StyleSheet.create({ | ||
container: { alignItems: "center", flex: 1, height: "100%", justifyContent: "center", width: "100%" }, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
import { useEffect, useRef, useState } from "react"; | ||
import { View } from "react-native"; | ||
import { VideoControlsOverlay } from "../VideoControlsOverlay"; | ||
import Player from "video.js/dist/types/player"; | ||
import { VideoViewProps } from "./VideoView"; | ||
import { styles } from "./styles"; | ||
import "./styles.web.css"; | ||
import videojs from "video.js"; | ||
|
||
declare const window: { | ||
videojs: typeof videojs; | ||
} & Window; | ||
|
||
const VideoView = ({ uri, testID }: VideoViewProps) => { | ||
const { videojs } = window; | ||
const videoRef = useRef<HTMLDivElement>(null); | ||
const playerRef = useRef<Player | null>(null); | ||
const [isControlsVisible, setIsControlsVisible] = useState(false); | ||
const [playbackStatus, setPlaybackStatus] = useState({ | ||
didJustFinish: false, | ||
isMuted: false, | ||
isPlaying: true, | ||
position: 0, | ||
duration: 1, | ||
playableDuration: 0, | ||
}); | ||
|
||
const updatePlaybackStatus = (updatedStatus: Partial<typeof playbackStatus>) => { | ||
setPlaybackStatus((prev) => ({ ...prev, ...updatedStatus })); | ||
}; | ||
|
||
const toggleControls = () => { | ||
setIsControlsVisible((prev) => !prev); | ||
}; | ||
|
||
const handlePlayPause = () => { | ||
if (playerRef.current?.paused()) { | ||
playerRef.current?.play(); | ||
} else { | ||
playerRef.current?.pause(); | ||
} | ||
}; | ||
|
||
const handleRW = () => { | ||
playerRef.current?.currentTime(playbackStatus.position / 1000 - 10); | ||
}; | ||
|
||
const handleFF = () => { | ||
playerRef.current?.currentTime(playbackStatus.position / 1000 + 10); | ||
}; | ||
|
||
const toggleMute = () => { | ||
const newValue = !playerRef.current?.muted(); | ||
playerRef.current?.muted(newValue); | ||
updatePlaybackStatus({ isMuted: newValue }); | ||
}; | ||
|
||
const handleReplay = () => { | ||
playerRef.current?.currentTime(0); | ||
}; | ||
|
||
const handleJumpTo = (position: number) => { | ||
playerRef.current?.tech().setCurrentTime(position / 1000); | ||
}; | ||
|
||
const options = { | ||
autoplay: true, | ||
controls: false, | ||
html5: { | ||
vhs: { | ||
overrideNative: true, | ||
}, | ||
}, | ||
sources: [ | ||
{ | ||
src: uri, | ||
}, | ||
], | ||
children: ["MediaLoader"], | ||
}; | ||
|
||
const onReady = (player: Player) => { | ||
playerRef.current = player; | ||
|
||
player.on("loadedmetadata", () => { | ||
updatePlaybackStatus({ | ||
duration: Math.floor(playerRef.current?.duration() ?? 0) * 1000, | ||
playableDuration: playerRef.current?.bufferedEnd(), | ||
}); | ||
}); | ||
|
||
player.on("play", () => { | ||
updatePlaybackStatus({ isPlaying: true, didJustFinish: false }); | ||
}); | ||
|
||
player.on("pause", () => { | ||
updatePlaybackStatus({ isPlaying: false }); | ||
}); | ||
|
||
player.on("timeupdate", () => { | ||
updatePlaybackStatus({ position: Math.floor(playerRef.current?.currentTime() ?? 0) * 1000 }); | ||
}); | ||
|
||
player.on("ended", () => { | ||
updatePlaybackStatus({ didJustFinish: true }); | ||
}); | ||
}; | ||
|
||
useEffect(() => { | ||
if (!videojs) return; | ||
|
||
if (!playerRef.current) { | ||
const videoElement = document.createElement("video-js"); | ||
|
||
videoElement.classList.add("vjs-big-play-centered"); | ||
videoRef.current?.appendChild(videoElement); | ||
|
||
const player = (playerRef.current = videojs(videoElement, options, () => { | ||
onReady && onReady(player); | ||
})); | ||
} else { | ||
const player = playerRef.current; | ||
|
||
player.options(options); | ||
} | ||
}, [options, videojs]); | ||
|
||
useEffect(() => { | ||
const player = playerRef.current; | ||
|
||
return () => { | ||
if (player && !player.isDisposed()) { | ||
player.dispose(); | ||
playerRef.current = null; | ||
} | ||
}; | ||
}, []); | ||
|
||
return ( | ||
<View style={styles.container}> | ||
<VideoControlsOverlay | ||
handlePlayPause={handlePlayPause} | ||
isPlaying={playbackStatus?.isPlaying} | ||
isVisible={isControlsVisible} | ||
onOverlayPress={toggleControls} | ||
handleRW={handleRW} | ||
handleFF={handleFF} | ||
duration={playbackStatus?.duration} | ||
availableDuration={playbackStatus?.playableDuration} | ||
position={playbackStatus?.position} | ||
toggleMute={toggleMute} | ||
isMute={playbackStatus?.isMuted} | ||
shouldReplay={playbackStatus?.didJustFinish} | ||
handleReplay={handleReplay} | ||
handleJumpTo={handleJumpTo} | ||
> | ||
<div style={{ position: "relative" }} ref={videoRef} data-testid={`${testID}-video-playback`} /> | ||
</VideoControlsOverlay> | ||
</View> | ||
); | ||
}; | ||
|
||
export default VideoView; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from "./VideoView"; |
Oops, something went wrong.