Skip to content

Commit

Permalink
poll video timer
Browse files Browse the repository at this point in the history
  • Loading branch information
JorrinKievit committed Aug 19, 2024
1 parent 6c5bb84 commit 3558cf2
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 18 deletions.
47 changes: 31 additions & 16 deletions apps/expo/src/components/player/BottomControls.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useCallback, useMemo, useState } from "react";
import { useCallback, useEffect, useMemo, useState } from "react";
import { Platform, TouchableOpacity } from "react-native";
import Animated, {
useAnimatedStyle,
Expand Down Expand Up @@ -27,29 +27,25 @@ export const BottomControls = () => {
const isLocalFile = usePlayerStore((state) => state.isLocalFile);
const [showRemaining, setShowRemaining] = useState(false);

const [localDuration, setLocalDuration] = useState(0);
const [localCurrentTime, setLocalCurrentTime] = useState(0);

const toggleTimeDisplay = useCallback(() => {
setIsIdle(false);
setShowRemaining(!showRemaining);
}, [showRemaining, setIsIdle]);

const { currentTime, remainingTime } = useMemo(() => {
if (player) {
const current = mapSecondsToTime(player.currentTime);
const remaining = `-${mapSecondsToTime(
(player.duration ?? 0) - (player.currentTime ?? 0),
)}`;
return { currentTime: current, remainingTime: remaining };
}
return {
currentTime: mapSecondsToTime(0),
remainingTime: mapSecondsToTime(0),
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [player?.currentTime]);
const current = mapSecondsToTime(localCurrentTime);
const remaining = `-${mapSecondsToTime(
(localDuration ?? 0) - localCurrentTime,
)}`;
return { currentTime: current, remainingTime: remaining };
}, [localCurrentTime, localDuration]);

const durationTime = useMemo(() => {
return mapSecondsToTime(player?.duration ?? 0);
}, [player?.duration]);
return mapSecondsToTime(localDuration ?? 0);
}, [localDuration]);

const translateY = useSharedValue(128);

Expand All @@ -63,6 +59,25 @@ export const BottomControls = () => {
};
});

// TODO: No duration events in expo-video yet
useEffect(() => {
const interval = setInterval(() => {
console.log(
"checking video duration and time",
player?.duration,
player?.currentTime,
);
if (player?.duration && player?.currentTime) {
setLocalDuration(player.duration);
setLocalCurrentTime(player.currentTime);
}
}, 1000);

return () => {
clearInterval(interval);
};
}, [player]);

return (
<Animated.View style={[animatedStyle, { height: 148 }]}>
<View
Expand Down
15 changes: 13 additions & 2 deletions apps/expo/src/components/player/PlayButton.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState } from "react";
import { useEffect, useState } from "react";
import { FontAwesome } from "@expo/vector-icons";
import { Spinner } from "tamagui";

Expand All @@ -10,10 +10,21 @@ export const PlayButton = () => {
const pauseAudio = usePlayerStore((state) => state.pauseAudio);

const [isPlaying, setIsPlaying] = useState(player?.playing ?? false);
const [isLoading, setIsLoading] = useState(false);

useEffect(() => {
const statusListener = player?.addListener("statusChange", (status) => {
setIsLoading(status === "loading");
});

return () => {
statusListener?.remove();
};
}, [player]);

if (!player) return null;

if (player.status === "loading") {
if (isLoading) {
return <Spinner size="large" color="white" />;
}

Expand Down

0 comments on commit 3558cf2

Please sign in to comment.