Skip to content

Commit b9c38d2

Browse files
committed
✨ feat: 重构播放控制逻辑,添加播放进度恢复功能并清理无用代码
1 parent d227ac8 commit b9c38d2

File tree

3 files changed

+5
-43
lines changed

3 files changed

+5
-43
lines changed

src/renderer/hooks/MusicHook.ts

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -873,24 +873,6 @@ export const initAudioListeners = async () => {
873873
if (finalSound) {
874874
// 更新全局 sound 引用
875875
sound.value = finalSound;
876-
877-
// 如果当前处于播放状态,启动进度更新
878-
if (playerStore.play) {
879-
// 如果有保存的播放进度,应用它
880-
if (playerStore.savedPlayProgress !== undefined) {
881-
try {
882-
// 设置音频位置
883-
finalSound.seek(playerStore.savedPlayProgress);
884-
// 同时更新时间显示
885-
nowTime.value = playerStore.savedPlayProgress;
886-
console.log('恢复播放进度:', playerStore.savedPlayProgress);
887-
} catch (e) {
888-
console.error('恢复播放进度失败:', e);
889-
}
890-
}
891-
892-
startProgressAnimation();
893-
}
894876
} else {
895877
console.warn('无法获取音频实例,跳过进度更新初始化');
896878
}

src/renderer/services/audioService.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ class AudioService {
456456
}
457457

458458
// 播放控制相关
459-
play(url?: string, track?: SongResult, isPlay: boolean = true): Promise<Howl> {
459+
play(url?: string, track?: SongResult, isPlay: boolean = true, seekTime: number = 0): Promise<Howl> {
460460
// 每次调用play方法时,尝试强制重置锁(注意:仅在页面刷新后的第一次播放时应用)
461461
if (!this.currentSound) {
462462
console.log('首次播放请求,强制重置操作锁');
@@ -599,6 +599,9 @@ class AudioService {
599599
// 音频加载成功后设置 EQ 和更新媒体会话
600600
if (this.currentSound) {
601601
try {
602+
if (seekTime > 0) {
603+
this.currentSound.seek(seekTime);
604+
}
602605
console.log('audioService: 音频加载成功,设置 EQ');
603606
await this.setupEQ(this.currentSound);
604607
this.updateMediaSessionMetadata(track);

src/renderer/store/modules/player.ts

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,6 @@ export const usePlayerStore = defineStore('player', () => {
395395
const musicFull = ref(false);
396396
const favoriteList = ref<Array<number | string>>(getLocalStorageItem('favoriteList', []));
397397
const dislikeList = ref<Array<number | string>>(getLocalStorageItem('dislikeList', []));
398-
const savedPlayProgress = ref<number | undefined>();
399398
const showSleepTimer = ref(false); // 定时弹窗
400399
// 添加播放列表抽屉状态
401400
const playListDrawerVisible = ref(false);
@@ -1080,7 +1079,6 @@ export const usePlayerStore = defineStore('player', () => {
10801079
const settingStore = useSettingsStore();
10811080
const savedPlayList = getLocalStorageItem('playList', []);
10821081
const savedPlayMusic = getLocalStorageItem<SongResult | null>('currentPlayMusic', null);
1083-
const savedProgress = localStorage.getItem('playProgress');
10841082

10851083
if (savedPlayList.length > 0) {
10861084
setPlayList(savedPlayList);
@@ -1100,20 +1098,6 @@ export const usePlayerStore = defineStore('player', () => {
11001098
}
11011099

11021100
await handlePlayMusic({ ...savedPlayMusic, isFirstPlay: true, playMusicUrl: undefined }, isPlaying);
1103-
1104-
if (savedProgress) {
1105-
try {
1106-
const progress = JSON.parse(savedProgress);
1107-
if (progress && progress.songId === savedPlayMusic.id) {
1108-
savedPlayProgress.value = progress.progress;
1109-
} else {
1110-
localStorage.removeItem('playProgress');
1111-
}
1112-
} catch (e) {
1113-
console.error('解析保存的播放进度失败', e);
1114-
localStorage.removeItem('playProgress');
1115-
}
1116-
}
11171101
} catch (error) {
11181102
console.error('重新获取音乐链接失败:', error);
11191103
play.value = false;
@@ -1200,13 +1184,7 @@ export const usePlayerStore = defineStore('player', () => {
12001184

12011185
// 播放新音频,传递是否应该播放的状态
12021186
console.log('调用audioService.play,播放状态:', shouldPlay);
1203-
const newSound = await audioService.play(playMusicUrl.value, playMusic.value, shouldPlay);
1204-
1205-
// 如果有保存的进度,设置播放位置
1206-
if (initialPosition > 0) {
1207-
newSound.seek(initialPosition);
1208-
}
1209-
1187+
const newSound = await audioService.play(playMusicUrl.value, playMusic.value, shouldPlay, initialPosition || 0);
12101188
// 发布音频就绪事件,让 MusicHook.ts 来处理设置监听器
12111189
window.dispatchEvent(new CustomEvent('audio-ready', { detail: { sound: newSound, shouldPlay } }));
12121190

@@ -1343,7 +1321,6 @@ export const usePlayerStore = defineStore('player', () => {
13431321
playListIndex,
13441322
playMode,
13451323
musicFull,
1346-
savedPlayProgress,
13471324
favoriteList,
13481325
dislikeList,
13491326
playListDrawerVisible,

0 commit comments

Comments
 (0)