fix slider
This commit is contained in:
@@ -215,7 +215,7 @@ const PlayerProvider = ({ children }) => {
|
||||
const [error, setError] = useState(null);
|
||||
|
||||
const autoPlayRef = useRef(false);
|
||||
const pendingSeekSecondsRef = useRef(null);
|
||||
const pendingSeekValueRef = useRef(null);
|
||||
|
||||
const source = useMemo(() => {
|
||||
if (!currentTrack?.source) return null;
|
||||
@@ -228,6 +228,11 @@ const PlayerProvider = ({ children }) => {
|
||||
? HIDDEN_ROUTE_NAMES.has(activeRouteName)
|
||||
: false;
|
||||
|
||||
const toPlayerSeekValue = useCallback((milliseconds) => {
|
||||
const ms = Math.max(0, Number(milliseconds) || 0);
|
||||
return Platform.OS === "web" ? ms : ms / 1000;
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
if (!status) return;
|
||||
|
||||
@@ -269,8 +274,8 @@ const PlayerProvider = ({ children }) => {
|
||||
|
||||
const run = async () => {
|
||||
try {
|
||||
const pendingSeek = pendingSeekSecondsRef.current;
|
||||
pendingSeekSecondsRef.current = null;
|
||||
const pendingSeek = pendingSeekValueRef.current;
|
||||
pendingSeekValueRef.current = null;
|
||||
|
||||
if (typeof pendingSeek === "number" && pendingSeek >= 0) {
|
||||
await player.seekTo?.(pendingSeek);
|
||||
@@ -305,7 +310,7 @@ const PlayerProvider = ({ children }) => {
|
||||
: typeof options?.positionMs === "number"
|
||||
? options.positionMs
|
||||
: 0;
|
||||
const targetSeconds = Math.max(0, targetPositionMs) / 1000;
|
||||
const targetSeekValue = toPlayerSeekValue(targetPositionMs);
|
||||
const shouldAutoPlay = options?.autoPlay !== false;
|
||||
|
||||
setError(null);
|
||||
@@ -313,8 +318,8 @@ const PlayerProvider = ({ children }) => {
|
||||
if (sameTrack) {
|
||||
setCurrentTrack((prev) => ({ ...prev, ...normalized }));
|
||||
try {
|
||||
if (targetSeconds > 0) {
|
||||
await player?.seekTo?.(targetSeconds);
|
||||
if (targetPositionMs > 0) {
|
||||
await player?.seekTo?.(targetSeekValue);
|
||||
}
|
||||
if (shouldAutoPlay) {
|
||||
await player?.play?.();
|
||||
@@ -342,7 +347,8 @@ const PlayerProvider = ({ children }) => {
|
||||
}
|
||||
|
||||
autoPlayRef.current = shouldAutoPlay;
|
||||
pendingSeekSecondsRef.current = targetSeconds > 0 ? targetSeconds : null;
|
||||
pendingSeekValueRef.current =
|
||||
targetPositionMs > 0 ? targetSeekValue : null;
|
||||
|
||||
setPlayback((prev) => ({
|
||||
...prev,
|
||||
@@ -352,7 +358,7 @@ const PlayerProvider = ({ children }) => {
|
||||
}));
|
||||
setCurrentTrack(normalized);
|
||||
},
|
||||
[currentTrack?.id, player]
|
||||
[currentTrack?.id, player, toPlayerSeekValue]
|
||||
);
|
||||
|
||||
const resume = useCallback(async () => {
|
||||
@@ -367,7 +373,7 @@ const PlayerProvider = ({ children }) => {
|
||||
const pause = useCallback(async () => {
|
||||
try {
|
||||
autoPlayRef.current = false;
|
||||
pendingSeekSecondsRef.current = null;
|
||||
pendingSeekValueRef.current = null;
|
||||
await player?.pause?.();
|
||||
} catch (err) {
|
||||
setError(err);
|
||||
@@ -405,19 +411,19 @@ const PlayerProvider = ({ children }) => {
|
||||
async (positionMs) => {
|
||||
if (!player) return;
|
||||
const bounded = Math.max(0, Number(positionMs) || 0);
|
||||
const seconds = bounded / 1000;
|
||||
const seekValue = toPlayerSeekValue(bounded);
|
||||
|
||||
try {
|
||||
if (status?.isLoaded) {
|
||||
await player.seekTo?.(seconds);
|
||||
await player.seekTo?.(seekValue);
|
||||
} else {
|
||||
pendingSeekSecondsRef.current = seconds;
|
||||
pendingSeekValueRef.current = seekValue;
|
||||
}
|
||||
} catch (err) {
|
||||
setError(err);
|
||||
}
|
||||
},
|
||||
[player, status?.isLoaded]
|
||||
[player, status?.isLoaded, toPlayerSeekValue]
|
||||
);
|
||||
|
||||
const seekBy = useCallback(
|
||||
@@ -431,7 +437,7 @@ const PlayerProvider = ({ children }) => {
|
||||
const stop = useCallback(async () => {
|
||||
try {
|
||||
autoPlayRef.current = false;
|
||||
pendingSeekSecondsRef.current = null;
|
||||
pendingSeekValueRef.current = null;
|
||||
await player?.pause?.();
|
||||
} catch (err) {
|
||||
setError(err);
|
||||
|
||||
Reference in New Issue
Block a user