fix(音频播放): 修复iOS设备在页面隐藏时音频播放逻辑问题
优化音频播放逻辑,当页面隐藏时不再延迟播放。同时修正自动播放下一首时的立即播放条件判断。
This commit is contained in:
12
index.html
12
index.html
@@ -967,6 +967,8 @@
|
||||
const playAudioWithFallback = (audio, options = {}) => {
|
||||
if (!audio) return;
|
||||
const { deferOnIOS = false } = options;
|
||||
const isHidden = typeof document !== 'undefined' && document.hidden;
|
||||
const shouldDefer = deferOnIOS && IS_IOS && !isHidden;
|
||||
const doPlay = () => {
|
||||
const playPromise = audio.play();
|
||||
if (playPromise && typeof playPromise.catch === 'function') {
|
||||
@@ -978,7 +980,7 @@
|
||||
}
|
||||
};
|
||||
|
||||
if (deferOnIOS && IS_IOS) {
|
||||
if (shouldDefer) {
|
||||
const onCanPlay = () => {
|
||||
audio.removeEventListener('canplay', onCanPlay);
|
||||
doPlay();
|
||||
@@ -1221,7 +1223,9 @@
|
||||
const triggerAutoNext = () => {
|
||||
if (autoAdvanceLockRef.current) return;
|
||||
autoAdvanceLockRef.current = true;
|
||||
playNext(true, { immediate: !IS_IOS, deferOnIOS: IS_IOS });
|
||||
const isHidden = typeof document !== 'undefined' && document.hidden;
|
||||
const immediate = IS_IOS || isHidden;
|
||||
playNext(true, { immediate, deferOnIOS: IS_IOS });
|
||||
};
|
||||
|
||||
const isNearEnd = () => {
|
||||
@@ -1377,7 +1381,9 @@
|
||||
}
|
||||
|
||||
if (auto) {
|
||||
autoNextPendingRef.current = true;
|
||||
autoNextPendingRef.current = !options.immediate;
|
||||
} else {
|
||||
autoNextPendingRef.current = false;
|
||||
}
|
||||
|
||||
let nextIdx;
|
||||
|
||||
Reference in New Issue
Block a user