1. iOS自动切歌问题(2处):
- isNearEnd:阈值 0.15s → 0.5s
- updateTime:移除 iOS 限制
2. 时长显示负值问题(2处):
- formatTime:增加异常值保护
- resolveDurationSeconds:防止返回负数
This commit is contained in:
14
index.html
14
index.html
@@ -159,7 +159,8 @@
|
||||
|
||||
// --- Utility Functions ---
|
||||
const formatTime = (seconds) => {
|
||||
if (!seconds) return "0:00";
|
||||
// 增加保护:处理负数、Infinity、NaN等异常值
|
||||
if (!Number.isFinite(seconds) || seconds < 0) return "0:00";
|
||||
const mins = Math.floor(seconds / 60);
|
||||
const secs = Math.floor(seconds % 60);
|
||||
return `${mins}:${secs < 10 ? '0' : ''}${secs}`;
|
||||
@@ -232,11 +233,14 @@
|
||||
if (audio.seekable && audio.seekable.length) {
|
||||
try {
|
||||
const end = audio.seekable.end(audio.seekable.length - 1);
|
||||
// 增加负数检查:iOS锁屏时可能返回负数
|
||||
if (Number.isFinite(end) && end > 0) return end;
|
||||
} catch (e) {}
|
||||
}
|
||||
}
|
||||
return getSongDurationSeconds(song);
|
||||
const fallbackDuration = getSongDurationSeconds(song);
|
||||
// 再次确保不会返回负数或异常值
|
||||
return Number.isFinite(fallbackDuration) && fallbackDuration > 0 ? fallbackDuration : 0;
|
||||
};
|
||||
|
||||
// --- ID Normalization ---
|
||||
@@ -1231,14 +1235,16 @@
|
||||
const isNearEnd = () => {
|
||||
const durationSeconds = resolveDurationSeconds(audio, currentSongRef.current);
|
||||
if (!Number.isFinite(durationSeconds) || durationSeconds <= 0) return false;
|
||||
const threshold = IS_IOS ? 0.15 : 0.35;
|
||||
// iOS锁屏时timeupdate频率降低,需要更大的提前量
|
||||
const threshold = IS_IOS ? 0.5 : 0.35;
|
||||
return audio.currentTime >= durationSeconds - threshold;
|
||||
};
|
||||
|
||||
const updateTime = () => {
|
||||
setCurrentTime(audio.currentTime);
|
||||
if (autoAdvanceLockRef.current) return;
|
||||
if (!IS_IOS && isNearEnd()) triggerAutoNext();
|
||||
// 移除iOS限制:所有平台都使用timeupdate检查,解决iOS锁屏时ended事件不触发的问题
|
||||
if (isNearEnd()) triggerAutoNext();
|
||||
};
|
||||
const updateDuration = () => setDuration(resolveDurationSeconds(audio, currentSongRef.current));
|
||||
const onEnded = () => triggerAutoNext();
|
||||
|
||||
Reference in New Issue
Block a user