mirror of
https://github.com/algerkong/AlgerMusicPlayer.git
synced 2026-04-14 23:11:00 +08:00
✨ feat: 添加音频URL过期事件监听,自动重新获取B站和网易云音乐音频URL并恢复播放
This commit is contained in:
@@ -866,3 +866,97 @@ export const initAudioListeners = async () => {
|
|||||||
console.error('初始化音频监听器失败:', error);
|
console.error('初始化音频监听器失败:', error);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// 监听URL过期事件,自动重新获取URL并恢复播放
|
||||||
|
audioService.on('url_expired', async (expiredTrack) => {
|
||||||
|
if (!expiredTrack) return;
|
||||||
|
|
||||||
|
console.log('检测到URL过期事件,准备重新获取URL', expiredTrack.name);
|
||||||
|
|
||||||
|
try {
|
||||||
|
const currentPosition = nowTime.value; // 保存当前播放进度
|
||||||
|
console.log('保存当前播放进度:', currentPosition);
|
||||||
|
|
||||||
|
// 处理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
|
||||||
|
);
|
||||||
|
|
||||||
|
console.log('成功获取新的B站URL:', newUrl);
|
||||||
|
|
||||||
|
// 更新存储
|
||||||
|
(expiredTrack as any).playMusicUrl = newUrl;
|
||||||
|
playerStore.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 (playerStore.play) {
|
||||||
|
newSound.play();
|
||||||
|
playerStore.setIsPlay(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
message.success('已自动恢复播放');
|
||||||
|
} catch (error) {
|
||||||
|
console.error('重新获取B站URL失败:', error);
|
||||||
|
message.error('重新获取音频地址失败,请手动点击播放');
|
||||||
|
}
|
||||||
|
} else if (expiredTrack.source === 'netease') {
|
||||||
|
// 处理网易云音乐,重新获取URL
|
||||||
|
console.log('重新获取网易云音乐URL');
|
||||||
|
try {
|
||||||
|
const { getSongUrl } = await import('@/store/modules/player');
|
||||||
|
const newUrl = await getSongUrl(expiredTrack.id, expiredTrack as any);
|
||||||
|
|
||||||
|
if (newUrl) {
|
||||||
|
console.log('成功获取新的网易云URL:', newUrl);
|
||||||
|
|
||||||
|
// 更新存储
|
||||||
|
(expiredTrack as any).playMusicUrl = newUrl;
|
||||||
|
playerStore.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 (playerStore.play) {
|
||||||
|
newSound.play();
|
||||||
|
playerStore.setIsPlay(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
message.success('已自动恢复播放');
|
||||||
|
} else {
|
||||||
|
throw new Error('获取URL失败');
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('重新获取网易云URL失败:', error);
|
||||||
|
message.error('重新获取音频地址失败,请手动点击播放');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('处理URL过期事件失败:', error);
|
||||||
|
message.error('恢复播放失败,请手动点击播放');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|||||||
@@ -422,6 +422,8 @@ class AudioService {
|
|||||||
console.log(`Retrying playback (${retryCount}/${maxRetries})...`);
|
console.log(`Retrying playback (${retryCount}/${maxRetries})...`);
|
||||||
setTimeout(tryPlay, 1000 * retryCount);
|
setTimeout(tryPlay, 1000 * retryCount);
|
||||||
} else {
|
} else {
|
||||||
|
// 发送URL过期事件,通知外部需要重新获取URL
|
||||||
|
this.emit('url_expired', this.currentTrack);
|
||||||
reject(new Error('音频加载失败,请尝试切换其他歌曲'));
|
reject(new Error('音频加载失败,请尝试切换其他歌曲'));
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -432,6 +434,8 @@ class AudioService {
|
|||||||
console.log(`Retrying playback (${retryCount}/${maxRetries})...`);
|
console.log(`Retrying playback (${retryCount}/${maxRetries})...`);
|
||||||
setTimeout(tryPlay, 1000 * retryCount);
|
setTimeout(tryPlay, 1000 * retryCount);
|
||||||
} else {
|
} else {
|
||||||
|
// 发送URL过期事件,通知外部需要重新获取URL
|
||||||
|
this.emit('url_expired', this.currentTrack);
|
||||||
reject(new Error('音频播放失败,请尝试切换其他歌曲'));
|
reject(new Error('音频播放失败,请尝试切换其他歌曲'));
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user