mirror of
https://github.com/algerkong/AlgerMusicPlayer.git
synced 2026-04-03 14:20:50 +08:00
✨ feat: 重构播放控制逻辑,添加播放进度恢复功能并清理无用代码
This commit is contained in:
@@ -873,24 +873,6 @@ export const initAudioListeners = async () => {
|
||||
if (finalSound) {
|
||||
// 更新全局 sound 引用
|
||||
sound.value = finalSound;
|
||||
|
||||
// 如果当前处于播放状态,启动进度更新
|
||||
if (playerStore.play) {
|
||||
// 如果有保存的播放进度,应用它
|
||||
if (playerStore.savedPlayProgress !== undefined) {
|
||||
try {
|
||||
// 设置音频位置
|
||||
finalSound.seek(playerStore.savedPlayProgress);
|
||||
// 同时更新时间显示
|
||||
nowTime.value = playerStore.savedPlayProgress;
|
||||
console.log('恢复播放进度:', playerStore.savedPlayProgress);
|
||||
} catch (e) {
|
||||
console.error('恢复播放进度失败:', e);
|
||||
}
|
||||
}
|
||||
|
||||
startProgressAnimation();
|
||||
}
|
||||
} else {
|
||||
console.warn('无法获取音频实例,跳过进度更新初始化');
|
||||
}
|
||||
|
||||
@@ -456,7 +456,7 @@ class AudioService {
|
||||
}
|
||||
|
||||
// 播放控制相关
|
||||
play(url?: string, track?: SongResult, isPlay: boolean = true): Promise<Howl> {
|
||||
play(url?: string, track?: SongResult, isPlay: boolean = true, seekTime: number = 0): Promise<Howl> {
|
||||
// 每次调用play方法时,尝试强制重置锁(注意:仅在页面刷新后的第一次播放时应用)
|
||||
if (!this.currentSound) {
|
||||
console.log('首次播放请求,强制重置操作锁');
|
||||
@@ -599,6 +599,9 @@ class AudioService {
|
||||
// 音频加载成功后设置 EQ 和更新媒体会话
|
||||
if (this.currentSound) {
|
||||
try {
|
||||
if (seekTime > 0) {
|
||||
this.currentSound.seek(seekTime);
|
||||
}
|
||||
console.log('audioService: 音频加载成功,设置 EQ');
|
||||
await this.setupEQ(this.currentSound);
|
||||
this.updateMediaSessionMetadata(track);
|
||||
|
||||
@@ -395,7 +395,6 @@ export const usePlayerStore = defineStore('player', () => {
|
||||
const musicFull = ref(false);
|
||||
const favoriteList = ref<Array<number | string>>(getLocalStorageItem('favoriteList', []));
|
||||
const dislikeList = ref<Array<number | string>>(getLocalStorageItem('dislikeList', []));
|
||||
const savedPlayProgress = ref<number | undefined>();
|
||||
const showSleepTimer = ref(false); // 定时弹窗
|
||||
// 添加播放列表抽屉状态
|
||||
const playListDrawerVisible = ref(false);
|
||||
@@ -1080,7 +1079,6 @@ export const usePlayerStore = defineStore('player', () => {
|
||||
const settingStore = useSettingsStore();
|
||||
const savedPlayList = getLocalStorageItem('playList', []);
|
||||
const savedPlayMusic = getLocalStorageItem<SongResult | null>('currentPlayMusic', null);
|
||||
const savedProgress = localStorage.getItem('playProgress');
|
||||
|
||||
if (savedPlayList.length > 0) {
|
||||
setPlayList(savedPlayList);
|
||||
@@ -1100,20 +1098,6 @@ export const usePlayerStore = defineStore('player', () => {
|
||||
}
|
||||
|
||||
await handlePlayMusic({ ...savedPlayMusic, isFirstPlay: true, playMusicUrl: undefined }, isPlaying);
|
||||
|
||||
if (savedProgress) {
|
||||
try {
|
||||
const progress = JSON.parse(savedProgress);
|
||||
if (progress && progress.songId === savedPlayMusic.id) {
|
||||
savedPlayProgress.value = progress.progress;
|
||||
} else {
|
||||
localStorage.removeItem('playProgress');
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('解析保存的播放进度失败', e);
|
||||
localStorage.removeItem('playProgress');
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('重新获取音乐链接失败:', error);
|
||||
play.value = false;
|
||||
@@ -1200,13 +1184,7 @@ export const usePlayerStore = defineStore('player', () => {
|
||||
|
||||
// 播放新音频,传递是否应该播放的状态
|
||||
console.log('调用audioService.play,播放状态:', shouldPlay);
|
||||
const newSound = await audioService.play(playMusicUrl.value, playMusic.value, shouldPlay);
|
||||
|
||||
// 如果有保存的进度,设置播放位置
|
||||
if (initialPosition > 0) {
|
||||
newSound.seek(initialPosition);
|
||||
}
|
||||
|
||||
const newSound = await audioService.play(playMusicUrl.value, playMusic.value, shouldPlay, initialPosition || 0);
|
||||
// 发布音频就绪事件,让 MusicHook.ts 来处理设置监听器
|
||||
window.dispatchEvent(new CustomEvent('audio-ready', { detail: { sound: newSound, shouldPlay } }));
|
||||
|
||||
@@ -1343,7 +1321,6 @@ export const usePlayerStore = defineStore('player', () => {
|
||||
playListIndex,
|
||||
playMode,
|
||||
musicFull,
|
||||
savedPlayProgress,
|
||||
favoriteList,
|
||||
dislikeList,
|
||||
playListDrawerVisible,
|
||||
|
||||
Reference in New Issue
Block a user