optimize volume and brightness overlays

This commit is contained in:
Jorrin
2024-03-21 21:58:41 +01:00
parent 86f1210090
commit 945a9bf21d
8 changed files with 119 additions and 111 deletions

View File

@@ -53,6 +53,7 @@ const defineConfig = (): ExpoConfig => ({
"expo-build-properties", "expo-build-properties",
{ {
android: { android: {
minSdkVersion: 24,
packagingOptions: { packagingOptions: {
pickFirst: [ pickFirst: [
"lib/x86/libcrypto.so", "lib/x86/libcrypto.so",

View File

@@ -27,31 +27,25 @@ import { isPointInSliderVicinity } from "./VideoSlider";
export const VideoPlayer = () => { export const VideoPlayer = () => {
const { const {
brightness, brightness,
debouncedBrightness,
showBrightnessOverlay, showBrightnessOverlay,
currentBrightness,
setShowBrightnessOverlay, setShowBrightnessOverlay,
handleBrightnessChange, handleBrightnessChange,
} = useBrightness(); } = useBrightness();
const { const { showVolumeOverlay, setShowVolumeOverlay, volume, currentVolume } =
currentVolume, useVolume();
debouncedVolume,
showVolumeOverlay,
setShowVolumeOverlay,
handleVolumeChange,
} = useVolume();
const { currentSpeed } = usePlaybackSpeed(); const { currentSpeed } = usePlaybackSpeed();
const { synchronizePlayback } = useAudioTrack(); const { synchronizePlayback } = useAudioTrack();
const { dismissFullscreenPlayer } = usePlayer(); const { dismissFullscreenPlayer } = usePlayer();
const [videoSrc, setVideoSrc] = useState<AVPlaybackSource>(); const [videoSrc, setVideoSrc] = useState<AVPlaybackSource>();
const [isLoading, setIsLoading] = useState(true); const [isLoading, setIsLoading] = useState(true);
const [resizeMode, setResizeMode] = useState(ResizeMode.CONTAIN); const [resizeMode, setResizeMode] = useState(ResizeMode.CONTAIN);
const [shouldPlay, setShouldPlay] = useState(true);
const [hasStartedPlaying, setHasStartedPlaying] = useState(false); const [hasStartedPlaying, setHasStartedPlaying] = useState(false);
const isGestureInSliderVicinity = useSharedValue(false); const isGestureInSliderVicinity = useSharedValue(false);
const router = useRouter(); const router = useRouter();
const scale = useSharedValue(1); const scale = useSharedValue(1);
const [lastVelocityY, setLastVelocityY] = useState(0);
const state = usePlayerStore((state) => state.interface.state);
const isIdle = usePlayerStore((state) => state.interface.isIdle); const isIdle = usePlayerStore((state) => state.interface.isIdle);
const stream = usePlayerStore((state) => state.interface.currentStream); const stream = usePlayerStore((state) => state.interface.currentStream);
const selectedAudioTrack = useAudioTrackStore((state) => state.selectedTrack); const selectedAudioTrack = useAudioTrackStore((state) => state.selectedTrack);
@@ -60,8 +54,8 @@ export const VideoPlayer = () => {
const setVideoRef = usePlayerStore((state) => state.setVideoRef); const setVideoRef = usePlayerStore((state) => state.setVideoRef);
const setStatus = usePlayerStore((state) => state.setStatus); const setStatus = usePlayerStore((state) => state.setStatus);
const setIsIdle = usePlayerStore((state) => state.setIsIdle); const setIsIdle = usePlayerStore((state) => state.setIsIdle);
const playAudio = usePlayerStore((state) => state.playAudio); const toggleAudio = usePlayerStore((state) => state.toggleAudio);
const pauseAudio = usePlayerStore((state) => state.pauseAudio); const toggleState = usePlayerStore((state) => state.toggleState);
const [gestureControlsEnabled, setGestureControlsEnabled] = useState(true); const [gestureControlsEnabled, setGestureControlsEnabled] = useState(true);
@@ -89,20 +83,12 @@ export const VideoPlayer = () => {
} }
}); });
const togglePlayback = () => {
setShouldPlay(!shouldPlay);
if (shouldPlay) {
void playAudio();
} else {
void pauseAudio();
}
};
const doubleTapGesture = Gesture.Tap() const doubleTapGesture = Gesture.Tap()
.enabled(gestureControlsEnabled) .enabled(gestureControlsEnabled)
.numberOfTaps(2) .numberOfTaps(2)
.onEnd(() => { .onEnd(() => {
runOnJS(togglePlayback)(); runOnJS(toggleAudio)();
runOnJS(toggleState)();
}); });
const screenHalfWidth = Dimensions.get("window").width / 2; const screenHalfWidth = Dimensions.get("window").width / 2;
@@ -114,6 +100,11 @@ export const VideoPlayer = () => {
if (isGestureInSliderVicinity.value) { if (isGestureInSliderVicinity.value) {
return; return;
} }
if (event.x > screenHalfWidth) {
runOnJS(setShowVolumeOverlay)(true);
} else {
runOnJS(setShowBrightnessOverlay)(true);
}
}) })
.onUpdate((event) => { .onUpdate((event) => {
const divisor = 5000; const divisor = 5000;
@@ -123,35 +114,25 @@ export const VideoPlayer = () => {
const directionMultiplier = event.velocityY < 0 ? 1 : -1; const directionMultiplier = event.velocityY < 0 ? 1 : -1;
const change = directionMultiplier * Math.abs(event.velocityY / divisor); const change = directionMultiplier * Math.abs(event.velocityY / divisor);
const newVolume = Math.max(0, Math.min(1, currentVolume.value + change));
const newBrightness = Math.max(0, Math.min(1, brightness.value + change));
if (event.x > screenHalfWidth) { if (event.x > screenHalfWidth) {
runOnJS(handleVolumeChange)(newVolume); const newVolume = Math.max(0, Math.min(1, volume.value + change));
volume.value = newVolume;
} else { } else {
const newBrightness = Math.max(
0,
Math.min(1, brightness.value + change),
);
brightness.value = newBrightness; brightness.value = newBrightness;
runOnJS(handleBrightnessChange)(newBrightness); runOnJS(handleBrightnessChange)(newBrightness);
} }
if (
(event.velocityY < 0 && lastVelocityY >= 0) ||
(event.velocityY >= 0 && lastVelocityY < 0)
) {
runOnJS(setLastVelocityY)(event.velocityY);
}
if (event.x > screenHalfWidth) {
runOnJS(handleVolumeChange)(newVolume);
runOnJS(setShowVolumeOverlay)(true);
} else {
runOnJS(handleBrightnessChange)(newBrightness);
runOnJS(setShowBrightnessOverlay)(true);
}
}) })
.onEnd(() => { .onEnd((event) => {
runOnJS(setLastVelocityY)(0); if (event.x > screenHalfWidth) {
runOnJS(setShowVolumeOverlay)(false); runOnJS(setShowVolumeOverlay)(false);
runOnJS(setShowBrightnessOverlay)(false); } else {
runOnJS(setShowBrightnessOverlay)(false);
}
}); });
const composedGesture = Gesture.Race( const composedGesture = Gesture.Race(
@@ -254,9 +235,9 @@ export const VideoPlayer = () => {
<Video <Video
ref={setVideoRef} ref={setVideoRef}
source={videoSrc} source={videoSrc}
shouldPlay={shouldPlay} shouldPlay={state === "playing"}
resizeMode={resizeMode} resizeMode={resizeMode}
volume={currentVolume.value} volume={volume.value}
rate={currentSpeed} rate={currentSpeed}
onLoadStart={onVideoLoadStart} onLoadStart={onVideoLoadStart}
onReadyForDisplay={onReadyForDisplay} onReadyForDisplay={onReadyForDisplay}
@@ -285,41 +266,51 @@ export const VideoPlayer = () => {
<Spinner <Spinner
size="large" size="large"
color="$loadingIndicator" color="$loadingIndicator"
style={{ position="absolute"
position: "absolute",
}}
/> />
)} )}
<ControlsOverlay isLoading={isLoading} /> <ControlsOverlay isLoading={isLoading} />
</View> </View>
{showVolumeOverlay && ( {showVolumeOverlay && <VolumeOverlay volume={currentVolume} />}
<View
position="absolute"
bottom={48}
alignSelf="center"
borderRadius={999}
backgroundColor="black"
padding={12}
opacity={0.5}
>
<Text fontWeight="bold">Volume: {debouncedVolume}</Text>
</View>
)}
{showBrightnessOverlay && ( {showBrightnessOverlay && (
<View <BrightnessOverlay brightness={currentBrightness} />
position="absolute"
bottom={48}
alignSelf="center"
borderRadius={999}
backgroundColor="black"
padding={12}
opacity={0.5}
>
<Text fontWeight="bold">Brightness: {debouncedBrightness}</Text>
</View>
)} )}
<CaptionRenderer /> <CaptionRenderer />
</View> </View>
</GestureDetector> </GestureDetector>
); );
}; };
function BrightnessOverlay(props: { brightness: number }) {
return (
<View
position="absolute"
bottom={48}
alignSelf="center"
borderRadius={999}
backgroundColor="black"
padding={12}
opacity={0.5}
>
<Text fontWeight="bold">
Brightness: {Math.round(props.brightness * 100)}%
</Text>
</View>
);
}
function VolumeOverlay(props: { volume: number }) {
return (
<View
position="absolute"
bottom={48}
alignSelf="center"
borderRadius={999}
backgroundColor="black"
padding={12}
opacity={0.5}
>
<Text fontWeight="bold">Volume: {Math.round(props.volume * 100)}%</Text>
</View>
);
}

View File

@@ -19,7 +19,7 @@ const PlayerText = styled(Text, {
function SettingsSheet(props: SheetProps) { function SettingsSheet(props: SheetProps) {
return ( return (
<Sheet <Sheet
snapPoints={[80]} snapPoints={[90]}
dismissOnSnapToBottom dismissOnSnapToBottom
modal modal
animation="spring" animation="spring"

View File

@@ -1,14 +1,18 @@
import { useCallback, useEffect, useState } from "react"; import { useCallback, useEffect, useMemo, useState } from "react";
import { useSharedValue } from "react-native-reanimated"; import { useSharedValue } from "react-native-reanimated";
import * as Brightness from "expo-brightness"; import * as Brightness from "expo-brightness";
import { useDebounceValue } from "tamagui";
import { useDebounce } from "../useDebounce";
export const useBrightness = () => { export const useBrightness = () => {
const [showBrightnessOverlay, setShowBrightnessOverlay] = useState(false); const [showBrightnessOverlay, setShowBrightnessOverlay] = useState(false);
const debouncedShowBrightnessOverlay = useDebounce(showBrightnessOverlay, 20);
const brightness = useSharedValue(0.5); const brightness = useSharedValue(0.5);
const debouncedBrightness = useDebounce(brightness.value, 20);
const currentBrightness = useDebounceValue(brightness.value, 20);
const memoizedBrightness = useMemo(
() => currentBrightness,
[currentBrightness],
);
useEffect(() => { useEffect(() => {
async function init() { async function init() {
@@ -26,24 +30,19 @@ export const useBrightness = () => {
void init(); void init();
}, [brightness]); }, [brightness]);
const handleBrightnessChange = useCallback( const handleBrightnessChange = useCallback(async (newValue: number) => {
async (newValue: number) => { try {
try { await Brightness.setBrightnessAsync(newValue);
setShowBrightnessOverlay(true); } catch (error) {
brightness.value = newValue; console.error("Failed to set brightness:", error);
await Brightness.setBrightnessAsync(newValue); }
} catch (error) { }, []);
console.error("Failed to set brightness:", error);
}
},
[brightness],
);
return { return {
showBrightnessOverlay: debouncedShowBrightnessOverlay, showBrightnessOverlay,
brightness,
debouncedBrightness: `${Math.round(debouncedBrightness * 100)}%`,
setShowBrightnessOverlay, setShowBrightnessOverlay,
brightness,
currentBrightness: memoizedBrightness,
handleBrightnessChange, handleBrightnessChange,
} as const; } as const;
}; };

View File

@@ -1,27 +1,19 @@
import { useCallback, useState } from "react"; import { useMemo, useState } from "react";
import { useSharedValue } from "react-native-reanimated"; import { useSharedValue } from "react-native-reanimated";
import { useDebounceValue } from "tamagui";
import { useDebounce } from "../useDebounce";
export const useVolume = () => { export const useVolume = () => {
const [showVolumeOverlay, setShowVolumeOverlay] = useState(false); const [showVolumeOverlay, setShowVolumeOverlay] = useState(false);
const debouncedShowVolumeOverlay = useDebounce(showVolumeOverlay, 20);
const volume = useSharedValue(1);
const debouncedVolume = useDebounce(volume.value, 20);
const handleVolumeChange = useCallback( const volume = useSharedValue(1);
(newValue: number) => {
volume.value = newValue; const currentVolume = useDebounceValue(volume.value, 20);
setShowVolumeOverlay(true); const memoizedVolume = useMemo(() => currentVolume, [currentVolume]);
},
[volume],
);
return { return {
showVolumeOverlay: debouncedShowVolumeOverlay, showVolumeOverlay,
currentVolume: volume,
debouncedVolume: `${Math.round(debouncedVolume * 100)}%`,
setShowVolumeOverlay, setShowVolumeOverlay,
handleVolumeChange, volume,
currentVolume: memoizedVolume,
} as const; } as const;
}; };

View File

@@ -11,6 +11,7 @@ export interface AudioSlice {
setCurrentAudioTrack(track: AudioTrack | null): void; setCurrentAudioTrack(track: AudioTrack | null): void;
playAudio(): Promise<void>; playAudio(): Promise<void>;
pauseAudio(): Promise<void>; pauseAudio(): Promise<void>;
toggleAudio(): Promise<void>;
setAudioPositionAsync(positionMillis: number): Promise<void>; setAudioPositionAsync(positionMillis: number): Promise<void>;
} }
@@ -36,6 +37,18 @@ export const createAudioSlice: MakeSlice<AudioSlice> = (set, get) => ({
await audioObject.pauseAsync(); await audioObject.pauseAsync();
} }
}, },
toggleAudio: async () => {
const { audioObject } = get();
if (audioObject) {
const status = await audioObject.getStatusAsync();
if (!status.isLoaded) return;
if (status.isPlaying) {
await audioObject.pauseAsync();
} else {
await audioObject.playAsync();
}
}
},
setAudioPositionAsync: async (positionMillis) => { setAudioPositionAsync: async (positionMillis) => {
const { audioObject } = get(); const { audioObject } = get();
if (audioObject) { if (audioObject) {

View File

@@ -10,8 +10,11 @@ export enum PlayerStatus {
READY, READY,
} }
type PlayerState = "playing" | "paused";
export interface InterfaceSlice { export interface InterfaceSlice {
interface: { interface: {
state: PlayerState;
isIdle: boolean; isIdle: boolean;
idleTimeout: NodeJS.Timeout | null; idleTimeout: NodeJS.Timeout | null;
currentStream: Stream | null; currentStream: Stream | null;
@@ -24,6 +27,7 @@ export interface InterfaceSlice {
audioTracks: AudioTrack[] | null; audioTracks: AudioTrack[] | null;
playerStatus: PlayerStatus; playerStatus: PlayerStatus;
}; };
toggleState(): void;
setIsIdle(state: boolean): void; setIsIdle(state: boolean): void;
setCurrentStream(stream: Stream): void; setCurrentStream(stream: Stream): void;
setAvailableStreams(streams: Stream[]): void; setAvailableStreams(streams: Stream[]): void;
@@ -38,6 +42,7 @@ export interface InterfaceSlice {
export const createInterfaceSlice: MakeSlice<InterfaceSlice> = (set, get) => ({ export const createInterfaceSlice: MakeSlice<InterfaceSlice> = (set, get) => ({
interface: { interface: {
state: "playing",
isIdle: true, isIdle: true,
idleTimeout: null, idleTimeout: null,
currentStream: null, currentStream: null,
@@ -50,6 +55,12 @@ export const createInterfaceSlice: MakeSlice<InterfaceSlice> = (set, get) => ({
audioTracks: null, audioTracks: null,
playerStatus: PlayerStatus.SCRAPING, playerStatus: PlayerStatus.SCRAPING,
}, },
toggleState: () => {
set((s) => {
s.interface.state =
s.interface.state === "playing" ? "paused" : "playing";
});
},
setIsIdle: (state) => { setIsIdle: (state) => {
set((s) => { set((s) => {
if (s.interface.idleTimeout) clearTimeout(s.interface.idleTimeout); if (s.interface.idleTimeout) clearTimeout(s.interface.idleTimeout);
@@ -108,6 +119,7 @@ export const createInterfaceSlice: MakeSlice<InterfaceSlice> = (set, get) => ({
reset: () => { reset: () => {
set(() => ({ set(() => ({
interface: { interface: {
state: "playing",
isIdle: true, isIdle: true,
idleTimeout: null, idleTimeout: null,
currentStream: null, currentStream: null,