fix: blur and music duration
This commit is contained in:
@@ -65,6 +65,7 @@ const useSharedAudioPlayer = (source, options = null) => {
|
||||
pause,
|
||||
resume,
|
||||
seekTo,
|
||||
ensureLoaded,
|
||||
isPlaying,
|
||||
positionMs,
|
||||
durationMs,
|
||||
@@ -84,10 +85,16 @@ const useSharedAudioPlayer = (source, options = null) => {
|
||||
};
|
||||
}, [isPlaying, positionMs, durationMs]);
|
||||
|
||||
const controlsRef = useRef({ play, pause, resume, seekTo });
|
||||
const controlsRef = useRef({
|
||||
play,
|
||||
pause,
|
||||
resume,
|
||||
seekTo,
|
||||
ensureLoaded,
|
||||
});
|
||||
useEffect(() => {
|
||||
controlsRef.current = { play, pause, resume, seekTo };
|
||||
}, [play, pause, resume, seekTo]);
|
||||
controlsRef.current = { play, pause, resume, seekTo, ensureLoaded };
|
||||
}, [play, pause, resume, seekTo, ensureLoaded]);
|
||||
|
||||
const playerRef = useRef(null);
|
||||
|
||||
@@ -138,6 +145,15 @@ const useSharedAudioPlayer = (source, options = null) => {
|
||||
},
|
||||
});
|
||||
|
||||
player.load = async ({ startPositionMs = 0 } = {}) => {
|
||||
if (typeof controlsRef.current.ensureLoaded !== "function") return;
|
||||
const target = Math.max(0, Number(startPositionMs) || 0);
|
||||
await controlsRef.current.ensureLoaded({
|
||||
startPositionMs: target,
|
||||
autoPlay: false,
|
||||
});
|
||||
};
|
||||
|
||||
playerRef.current = player;
|
||||
}
|
||||
}, [descriptor]);
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { useCallback, useMemo } from "react";
|
||||
import { useCallback, useMemo, useRef } from "react";
|
||||
import usePlayer from "./usePlayer";
|
||||
|
||||
const useTrackController = (descriptor) => {
|
||||
@@ -19,10 +19,38 @@ const useTrackController = (descriptor) => {
|
||||
return descriptor;
|
||||
}, [descriptor]);
|
||||
|
||||
const lastKnownRef = useRef({
|
||||
descriptorId: null,
|
||||
positionMs: 0,
|
||||
durationMs: 0,
|
||||
});
|
||||
|
||||
const trackId = normalizedDescriptor?.id || null;
|
||||
const isCurrent = trackId ? currentTrack?.id === trackId : false;
|
||||
const effectivePositionMs = isCurrent ? positionMs : 0;
|
||||
const effectiveDurationMs = isCurrent ? durationMs : 0;
|
||||
const descriptorId = normalizedDescriptor?.id ?? null;
|
||||
|
||||
if (lastKnownRef.current.descriptorId !== descriptorId) {
|
||||
lastKnownRef.current = {
|
||||
descriptorId,
|
||||
positionMs: 0,
|
||||
durationMs: 0,
|
||||
};
|
||||
}
|
||||
|
||||
if (isCurrent) {
|
||||
lastKnownRef.current = {
|
||||
descriptorId,
|
||||
positionMs,
|
||||
durationMs,
|
||||
};
|
||||
}
|
||||
|
||||
const effectivePositionMs = isCurrent
|
||||
? positionMs
|
||||
: lastKnownRef.current.positionMs;
|
||||
const effectiveDurationMs = isCurrent
|
||||
? durationMs
|
||||
: lastKnownRef.current.durationMs;
|
||||
const effectiveIsPlaying = isCurrent && isPlaying;
|
||||
|
||||
const ensureLoaded = useCallback(
|
||||
@@ -140,4 +168,3 @@ const useTrackController = (descriptor) => {
|
||||
};
|
||||
|
||||
export default useTrackController;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user