update(AudioPlayer): 音频结束判断,处理 audio.ended 失效情况

feature/qrcode
mackt 6 months ago
parent eeb8df5b30
commit 8b4a2f1362

@ -121,13 +121,15 @@ export default function AudioPlayer({
// 监听音频进度 // 监听音频进度
const listenTimeUpdate = () => { const listenTimeUpdate = () => {
const currentTime = Math.ceil(audioRef.current.currentTime) ?? 0;
// 完成播放 // 完成播放
if (audioRef.current.ended) { if (audioRef.current.ended || currentTime >= audioRef.current.duration) {
handleSwitchAudio(1); handleSwitchAudio(1);
} }
if (!isDrag.current) { if (!isDrag.current) {
setTrackProgress(audioRef.current.currentTime); setTrackProgress(currentTime);
} }
}; };
@ -187,7 +189,6 @@ export default function AudioPlayer({
await new Promise((resolve, reject) => { await new Promise((resolve, reject) => {
audioRef.current.oncanplaythrough = resolve; audioRef.current.oncanplaythrough = resolve;
audioRef.current.onerror = reject; audioRef.current.onerror = reject;
audioRef.current.addEventListener('timeupdate', listenTimeUpdate);
}); });
} catch (error) { } catch (error) {
console.error('Error downloading and playing audio:', error); console.error('Error downloading and playing audio:', error);
@ -195,9 +196,10 @@ export default function AudioPlayer({
}; };
const initAudioMeta = () => { const initAudioMeta = () => {
setMediaSession(); // 设置 MediaSession setMediaSession();
handleSetVolume(volume); // 设置音量 handleSetVolume(volume);
setDuration(audioRef.current.duration); // 设置音频时长 setDuration(audioRef.current.duration);
audioRef.current.addEventListener('timeupdate', listenTimeUpdate);
}; };
const switchNewAudio = async () => { const switchNewAudio = async () => {

Loading…
Cancel
Save