2026-03-19 01:12:17 +08:00
|
|
|
|
import { useAudioPlayer, useAudioPlayerStatus } from 'expo-audio';
|
|
|
|
|
|
import { useCallback, useEffect, useRef, useState } from 'react';
|
|
|
|
|
|
|
|
|
|
|
|
import { audioFocus } from '@/core/audio/audio-focus';
|
|
|
|
|
|
|
|
|
|
|
|
import type { PlaybackItem, PlayerStatus } from '../types';
|
|
|
|
|
|
|
|
|
|
|
|
interface UsePlayerResult {
|
|
|
|
|
|
status: PlayerStatus;
|
|
|
|
|
|
queueLength: number;
|
2026-03-20 16:36:42 +08:00
|
|
|
|
/** Current playback source URI (file, https, or data URL). */
|
|
|
|
|
|
currentSource: string | null;
|
2026-03-19 01:12:17 +08:00
|
|
|
|
enqueue: (item: PlaybackItem) => void;
|
2026-03-20 16:36:42 +08:00
|
|
|
|
/** Replace queue and play this item (e.g. user voice bubble vs other sources). */
|
|
|
|
|
|
enqueueExclusive: (item: PlaybackItem) => Promise<void>;
|
2026-03-19 01:12:17 +08:00
|
|
|
|
stop: () => void;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Self-contained audio playback hook with queue management.
|
|
|
|
|
|
*
|
|
|
|
|
|
* - Uses useAudioPlayer + useAudioPlayerStatus for native playback
|
|
|
|
|
|
* - Detects playback completion to auto-advance the queue
|
|
|
|
|
|
* - Coordinates with audioFocus for recording/playback mutual exclusion
|
|
|
|
|
|
* - Subscribes to audioFocus owner changes to resume after recording ends
|
|
|
|
|
|
*/
|
|
|
|
|
|
export function usePlayer(): UsePlayerResult {
|
|
|
|
|
|
const queueRef = useRef<PlaybackItem[]>([]);
|
|
|
|
|
|
const [status, setStatus] = useState<PlayerStatus>('idle');
|
|
|
|
|
|
const [queueLength, setQueueLength] = useState(0);
|
|
|
|
|
|
const [currentSource, setCurrentSource] = useState<string | null>(null);
|
|
|
|
|
|
const isPlayingRef = useRef(false);
|
|
|
|
|
|
const wasBlockedByRecorderRef = useRef(false);
|
2026-03-19 10:24:48 +08:00
|
|
|
|
const isPlayNextInProgressRef = useRef(false);
|
2026-03-20 16:36:42 +08:00
|
|
|
|
/** 同步反映「当前是否正在播放某条 URI」;enqueue 不能依赖 state,否则 await stop() 后仍为陈旧闭包。 */
|
|
|
|
|
|
const playbackActiveUriRef = useRef<string | null>(null);
|
|
|
|
|
|
/** 当前 source 是否已进入过 playing=true,避免换源瞬间 playerStatus 仍带上一首的 duration 而误判「已播完」。 */
|
|
|
|
|
|
const trackHasPlayedRef = useRef(false);
|
2026-03-19 01:12:17 +08:00
|
|
|
|
|
2026-03-20 16:36:42 +08:00
|
|
|
|
const player = useAudioPlayer(currentSource, { downloadFirst: false });
|
2026-03-19 01:12:17 +08:00
|
|
|
|
const playerStatus = useAudioPlayerStatus(player);
|
|
|
|
|
|
|
|
|
|
|
|
// Start playback when a new source is set
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
if (currentSource && player) {
|
|
|
|
|
|
player.play();
|
|
|
|
|
|
isPlayingRef.current = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
}, [currentSource, player]);
|
|
|
|
|
|
|
|
|
|
|
|
const playNext = useCallback(async () => {
|
2026-03-19 10:24:48 +08:00
|
|
|
|
if (isPlayNextInProgressRef.current) return;
|
|
|
|
|
|
isPlayNextInProgressRef.current = true;
|
|
|
|
|
|
try {
|
|
|
|
|
|
if (queueRef.current.length === 0) {
|
2026-03-20 16:36:42 +08:00
|
|
|
|
playbackActiveUriRef.current = null;
|
2026-03-19 10:24:48 +08:00
|
|
|
|
setCurrentSource(null);
|
|
|
|
|
|
setStatus('idle');
|
|
|
|
|
|
setQueueLength(0);
|
|
|
|
|
|
await audioFocus.release();
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2026-03-19 01:12:17 +08:00
|
|
|
|
|
2026-03-19 10:24:48 +08:00
|
|
|
|
const acquired = await audioFocus.acquireForPlayback();
|
|
|
|
|
|
if (!acquired) {
|
|
|
|
|
|
setStatus('idle');
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (queueRef.current.length === 0) return;
|
2026-03-19 01:12:17 +08:00
|
|
|
|
|
2026-03-19 10:24:48 +08:00
|
|
|
|
const next = queueRef.current.shift()!;
|
|
|
|
|
|
setQueueLength(queueRef.current.length);
|
|
|
|
|
|
setStatus('playing');
|
2026-03-20 16:36:42 +08:00
|
|
|
|
trackHasPlayedRef.current = false;
|
|
|
|
|
|
playbackActiveUriRef.current = next.uri;
|
2026-03-19 10:24:48 +08:00
|
|
|
|
setCurrentSource(next.uri);
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
isPlayNextInProgressRef.current = false;
|
|
|
|
|
|
}
|
2026-03-19 01:12:17 +08:00
|
|
|
|
}, []);
|
|
|
|
|
|
|
2026-03-20 16:36:42 +08:00
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
if (playerStatus.playing) {
|
|
|
|
|
|
trackHasPlayedRef.current = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
}, [playerStatus.playing]);
|
|
|
|
|
|
|
|
|
|
|
|
// Detect playback completion → advance queue(必须曾 playing,避免换源瞬间沿用上一条的 duration/currentTime)
|
2026-03-19 01:12:17 +08:00
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
if (!currentSource || !isPlayingRef.current) return;
|
|
|
|
|
|
|
|
|
|
|
|
const { playing, currentTime, duration } = playerStatus;
|
2026-03-20 16:36:42 +08:00
|
|
|
|
const finished =
|
|
|
|
|
|
trackHasPlayedRef.current &&
|
|
|
|
|
|
!playing &&
|
|
|
|
|
|
duration > 0 &&
|
|
|
|
|
|
currentTime >= duration - 0.05;
|
2026-03-19 01:12:17 +08:00
|
|
|
|
|
|
|
|
|
|
if (finished) {
|
2026-03-20 16:36:42 +08:00
|
|
|
|
trackHasPlayedRef.current = false;
|
2026-03-19 01:12:17 +08:00
|
|
|
|
isPlayingRef.current = false;
|
|
|
|
|
|
playNext();
|
|
|
|
|
|
}
|
|
|
|
|
|
}, [playerStatus, currentSource, playNext]);
|
|
|
|
|
|
|
|
|
|
|
|
// Subscribe to audioFocus owner changes for recorder → idle recovery
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
const unsub = audioFocus.onOwnerChange((owner) => {
|
|
|
|
|
|
if (owner === 'recorder') {
|
|
|
|
|
|
wasBlockedByRecorderRef.current =
|
|
|
|
|
|
queueRef.current.length > 0 || currentSource !== null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (owner === null && wasBlockedByRecorderRef.current) {
|
|
|
|
|
|
wasBlockedByRecorderRef.current = false;
|
|
|
|
|
|
if (queueRef.current.length > 0 && status === 'idle') {
|
|
|
|
|
|
playNext();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
return unsub;
|
|
|
|
|
|
}, [status, currentSource, playNext]);
|
|
|
|
|
|
|
|
|
|
|
|
const enqueue = useCallback(
|
|
|
|
|
|
async (item: PlaybackItem) => {
|
|
|
|
|
|
queueRef.current.push(item);
|
|
|
|
|
|
setQueueLength(queueRef.current.length);
|
|
|
|
|
|
|
2026-03-20 16:36:42 +08:00
|
|
|
|
const shouldKick =
|
|
|
|
|
|
queueRef.current.length === 1 && playbackActiveUriRef.current === null;
|
|
|
|
|
|
|
|
|
|
|
|
if (shouldKick) {
|
2026-03-19 01:12:17 +08:00
|
|
|
|
await playNext();
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
2026-03-20 16:36:42 +08:00
|
|
|
|
[playNext],
|
2026-03-19 01:12:17 +08:00
|
|
|
|
);
|
|
|
|
|
|
|
2026-03-20 16:36:42 +08:00
|
|
|
|
const enqueueExclusive = useCallback(
|
|
|
|
|
|
async (item: PlaybackItem) => {
|
|
|
|
|
|
queueRef.current = [item];
|
|
|
|
|
|
setQueueLength(1);
|
|
|
|
|
|
isPlayingRef.current = false;
|
|
|
|
|
|
if (player) {
|
|
|
|
|
|
player.pause();
|
|
|
|
|
|
}
|
|
|
|
|
|
playbackActiveUriRef.current = null;
|
|
|
|
|
|
setCurrentSource(null);
|
|
|
|
|
|
setStatus('idle');
|
|
|
|
|
|
await audioFocus.release();
|
|
|
|
|
|
await playNext();
|
2026-03-19 01:12:17 +08:00
|
|
|
|
},
|
2026-03-20 16:36:42 +08:00
|
|
|
|
[player, playNext],
|
2026-03-19 01:12:17 +08:00
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
const stop = useCallback(async () => {
|
|
|
|
|
|
queueRef.current = [];
|
|
|
|
|
|
setQueueLength(0);
|
|
|
|
|
|
isPlayingRef.current = false;
|
|
|
|
|
|
|
|
|
|
|
|
if (player) {
|
|
|
|
|
|
player.pause();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-20 16:36:42 +08:00
|
|
|
|
playbackActiveUriRef.current = null;
|
2026-03-19 01:12:17 +08:00
|
|
|
|
setCurrentSource(null);
|
|
|
|
|
|
setStatus('idle');
|
|
|
|
|
|
await audioFocus.release();
|
|
|
|
|
|
}, [player]);
|
|
|
|
|
|
|
2026-03-20 16:36:42 +08:00
|
|
|
|
return {
|
|
|
|
|
|
status,
|
|
|
|
|
|
queueLength,
|
|
|
|
|
|
currentSource,
|
|
|
|
|
|
enqueue,
|
|
|
|
|
|
enqueueExclusive,
|
|
|
|
|
|
stop,
|
|
|
|
|
|
};
|
2026-03-19 01:12:17 +08:00
|
|
|
|
}
|