feat: 优化播放检测逻辑

This commit is contained in:
algerkong
2025-12-20 14:16:32 +08:00
parent 77bb06c0d6
commit 999cd6526b
3 changed files with 37 additions and 90 deletions
+15 -81
View File
@@ -2,11 +2,9 @@ import { cloneDeep } from 'lodash';
import { createDiscreteApi } from 'naive-ui';
import { computed, type ComputedRef, nextTick, onUnmounted, ref, watch } from 'vue';
import { getBilibiliAudioUrl } from '@/api/bilibili';
import useIndexedDB from '@/hooks/IndexDBHook';
import { audioService } from '@/services/audioService';
import type { usePlayerStore } from '@/store';
import { getSongUrl } from '@/store/modules/player';
import type { Artist, ILyricText, SongResult } from '@/types/music';
import { isElectron } from '@/utils';
import { getTextColors } from '@/utils/linearColor';
@@ -574,7 +572,12 @@ const setupAudioListeners = () => {
};
export const play = () => {
audioService.getCurrentSound()?.play();
const currentSound = audioService.getCurrentSound();
if (currentSound) {
currentSound.play();
// 在播放时也进行状态检测,防止URL已过期导致无声
getPlayerStore().checkPlaybackState(getPlayerStore().playMusic);
}
};
export const pause = () => {
@@ -1042,86 +1045,17 @@ audioService.on('url_expired', async (expiredTrack) => {
console.log('检测到URL过期事件,准备重新获取URL', expiredTrack.name);
try {
const currentPosition = nowTime.value; // 保存当前播放进度
console.log('保存当前播放进度:', currentPosition);
// 使用 handlePlayMusic 重新播放,它会自动处理 URL 获取和状态跟踪
// 我们将 isFirstPlay 设为 true 以强制获取新 URL
const trackToPlay = {
...expiredTrack,
isFirstPlay: true,
playMusicUrl: undefined
};
// 处理B站视频
if (expiredTrack.source === 'bilibili' && expiredTrack.bilibiliData) {
console.log('重新获取B站视频URL');
try {
// 使用API中的函数获取B站音频URL
const newUrl = await getBilibiliAudioUrl(
expiredTrack.bilibiliData.bvid,
expiredTrack.bilibiliData.cid
);
await getPlayerStore().handlePlayMusic(trackToPlay, getPlayerStore().play);
console.log('成功获取新的B站URL:', newUrl);
// 更新存储
(expiredTrack as any).playMusicUrl = newUrl;
getPlayerStore().playMusicUrl = newUrl;
// 重新播放并设置进度
const newSound = await audioService.play(newUrl, expiredTrack);
sound.value = newSound as Howl;
// 恢复播放进度
if (currentPosition > 0) {
newSound.seek(currentPosition);
nowTime.value = currentPosition;
console.log('恢复播放进度:', currentPosition);
}
// 如果之前是播放状态,继续播放
if (getPlayerStore().play) {
newSound.play();
getPlayerStore().setIsPlay(true);
}
message.success('已自动恢复播放');
} catch (error) {
console.error('重新获取B站URL失败:', error);
message.error('重新获取音频地址失败,请手动点击播放');
}
} else if (expiredTrack.source === 'netease') {
// 处理网易云音乐,重新获取URL
console.log('重新获取网易云音乐URL');
try {
const newUrl = await getSongUrl(expiredTrack.id, expiredTrack as any);
if (newUrl) {
console.log('成功获取新的网易云URL:', newUrl);
// 更新存储
(expiredTrack as any).playMusicUrl = newUrl;
getPlayerStore().playMusicUrl = newUrl;
// 重新播放并设置进度
const newSound = await audioService.play(newUrl, expiredTrack);
sound.value = newSound as Howl;
// 恢复播放进度
if (currentPosition > 0) {
newSound.seek(currentPosition);
nowTime.value = currentPosition;
console.log('恢复播放进度:', currentPosition);
}
// 如果之前是播放状态,继续播放
if (getPlayerStore().play) {
newSound.play();
getPlayerStore().setIsPlay(true);
}
message.success('已自动恢复播放');
} else {
throw new Error('获取URL失败');
}
} catch (error) {
console.error('重新获取网易云URL失败:', error);
message.error('重新获取音频地址失败,请手动点击播放');
}
}
message.success('已自动恢复播放');
} catch (error) {
console.error('处理URL过期事件失败:', error);
message.error('恢复播放失败,请手动点击播放');