feat: playback speed changing

This commit is contained in:
Adrian Castro
2024-03-01 14:47:35 +01:00
parent 9c00fc2f54
commit ae760a4b9b
4 changed files with 94 additions and 0 deletions

View File

@@ -6,6 +6,7 @@ import { Text } from "../ui/Text";
import { AudioTrackSelector } from "./AudioTrackSelector"; import { AudioTrackSelector } from "./AudioTrackSelector";
import { CaptionsSelector } from "./CaptionsSelector"; import { CaptionsSelector } from "./CaptionsSelector";
import { Controls } from "./Controls"; import { Controls } from "./Controls";
import { PlaybackSpeedSelector } from "./PlaybackSpeedSelector";
import { ProgressBar } from "./ProgressBar"; import { ProgressBar } from "./ProgressBar";
import { SeasonSelector } from "./SeasonEpisodeSelector"; import { SeasonSelector } from "./SeasonEpisodeSelector";
import { SourceSelector } from "./SourceSelector"; import { SourceSelector } from "./SourceSelector";
@@ -62,6 +63,7 @@ export const BottomControls = () => {
<CaptionsSelector /> <CaptionsSelector />
<SourceSelector /> <SourceSelector />
<AudioTrackSelector /> <AudioTrackSelector />
<PlaybackSpeedSelector />
</View> </View>
</View> </View>
); );

View File

@@ -0,0 +1,71 @@
import { Pressable, ScrollView, View } from "react-native";
import Modal from "react-native-modal";
import { MaterialCommunityIcons } from "@expo/vector-icons";
import colors from "@movie-web/tailwind-config/colors";
import { usePlaybackSpeed } from "~/hooks/player/usePlaybackSpeed";
import { useBoolean } from "~/hooks/useBoolean";
import { Button } from "../ui/Button";
import { Text } from "../ui/Text";
import { Controls } from "./Controls";
export const PlaybackSpeedSelector = () => {
const { currentSpeed, changePlaybackSpeed } = usePlaybackSpeed();
const { isTrue, on, off } = useBoolean();
const speeds = [0.5, 0.75, 1, 1.25, 1.5, 1.75, 2];
return (
<View className="max-w-36 flex-1">
<Controls>
<Button
title="Speed"
variant="outline"
onPress={on}
iconLeft={
<MaterialCommunityIcons
name="speedometer"
size={24}
color={colors.primary[300]}
/>
}
/>
</Controls>
<Modal
isVisible={isTrue}
onBackdropPress={off}
supportedOrientations={["portrait", "landscape"]}
style={{
width: "35%",
justifyContent: "center",
alignSelf: "center",
}}
>
<ScrollView className="flex-1 bg-gray-900">
<Text className="text-center font-bold">Select speed</Text>
{speeds.map((speed) => (
<Pressable
className="flex w-full flex-row justify-between p-3"
key={speed}
onPress={() => {
changePlaybackSpeed(speed);
off();
}}
>
<Text>{speed}</Text>
{speed === currentSpeed.value && (
<MaterialCommunityIcons
name="check-circle"
size={24}
color={colors.primary[300]}
/>
)}
</Pressable>
))}
</ScrollView>
</Modal>
</View>
);
};

View File

@@ -18,6 +18,7 @@ import * as StatusBar from "expo-status-bar";
import { findHighestQuality } from "@movie-web/provider-utils"; import { findHighestQuality } from "@movie-web/provider-utils";
import { useBrightness } from "~/hooks/player/useBrightness"; import { useBrightness } from "~/hooks/player/useBrightness";
import { usePlaybackSpeed } from "~/hooks/player/usePlaybackSpeed";
import { usePlayer } from "~/hooks/player/usePlayer"; import { usePlayer } from "~/hooks/player/usePlayer";
import { useVolume } from "~/hooks/player/useVolume"; import { useVolume } from "~/hooks/player/useVolume";
import { useAudioTrackStore } from "~/stores/audio"; import { useAudioTrackStore } from "~/stores/audio";
@@ -41,6 +42,7 @@ export const VideoPlayer = () => {
setShowVolumeOverlay, setShowVolumeOverlay,
handleVolumeChange, handleVolumeChange,
} = useVolume(); } = useVolume();
const { currentSpeed } = usePlaybackSpeed();
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);
@@ -257,6 +259,7 @@ export const VideoPlayer = () => {
shouldPlay={shouldPlay} shouldPlay={shouldPlay}
resizeMode={resizeMode} resizeMode={resizeMode}
volume={currentVolume.value} volume={currentVolume.value}
rate={currentSpeed.value}
onLoadStart={onVideoLoadStart} onLoadStart={onVideoLoadStart}
onReadyForDisplay={onReadyForDisplay} onReadyForDisplay={onReadyForDisplay}
onPlaybackStatusUpdate={setStatus} onPlaybackStatusUpdate={setStatus}

View File

@@ -0,0 +1,18 @@
import { useCallback } from "react";
import { useSharedValue } from "react-native-reanimated";
export const usePlaybackSpeed = () => {
const speed = useSharedValue(1);
const changePlaybackSpeed = useCallback(
(newValue: number) => {
speed.value = newValue;
},
[speed],
);
return {
currentSpeed: speed,
changePlaybackSpeed,
} as const;
};