Skip to content

Commit b2dcec8

Browse files
authored
Merge pull request #301 from serious-snow/dev
refactor(music): 优化音乐播放逻辑和数据处理
2 parents 6f93d65 + 49f462c commit b2dcec8

File tree

4 files changed

+30
-12
lines changed

4 files changed

+30
-12
lines changed

src/stores/data.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,15 +160,23 @@ export const useDataStore = defineStore({
160160
},
161161
// 新增下一首播放歌曲
162162
async setNextPlaySong(song: SongType, index: number): Promise<number> {
163-
// 移除重复的歌曲(如果存在)
164-
const playList = this.playList.filter((item) => item.id !== song.id);
163+
// 若为空,则直接添加
164+
if (this.playList.length === 0) {
165+
this.playList = [song];
166+
await musicDB.setItem("playList", cloneDeep(this.playList));
167+
return 0;
168+
}
169+
165170
// 在当前播放位置之后插入歌曲
166-
const indexAdd = index + 1;
167-
playList.splice(indexAdd, 0, song);
171+
const indexAdd = index + 1
172+
this.playList.splice(indexAdd, 0, song)
173+
// 移除重复的歌曲(如果存在)
174+
const playList = this.playList.filter((item,idx) => idx === indexAdd || item.id !== song.id);
168175
// 更新本地存储
169176
this.playList = playList;
170177
await musicDB.setItem("playList", cloneDeep(playList));
171-
return indexAdd;
178+
// 返回刚刚插入的歌曲索引
179+
return playList.findIndex(item => item.id === song.id);
172180
},
173181
// 更改播放历史
174182
async setHistory(song: SongType) {

src/stores/music.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ export const useMusicStore = defineStore({
8686
actions: {
8787
// 恢复默认音乐数据
8888
resetMusicData() {
89-
this.playSong = defaultMusicData;
89+
this.playSong = {...defaultMusicData};
9090
this.songLyric = {
9191
lrcData: [],
9292
yrcData: [],

src/stores/status.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export const useStatusStore = defineStore({
7474
// 音乐频谱数据
7575
spectrumsData: [],
7676
// 当前播放索引
77-
playIndex: 0,
77+
playIndex: -1,
7878
// 歌词播放索引
7979
lyricIndex: -1,
8080
// 默认倍速

src/utils/player.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,12 @@ class Player {
588588
*/
589589
async pause(changeStatus: boolean = true) {
590590
const statusStore = useStatusStore();
591+
592+
// 播放器未加载完成
593+
if (this.player.state() !== "loaded"){
594+
return
595+
}
596+
591597
// 淡出
592598
await new Promise<void>((resolve) => {
593599
this.player.fade(statusStore.playVolume, 0, this.getFadeTime());
@@ -873,23 +879,24 @@ class Player {
873879
// 尝试添加
874880
const songIndex = await dataStore.setNextPlaySong(song, statusStore.playIndex);
875881
// 播放歌曲
876-
if (!songIndex) return;
877-
if (play) this.togglePlayIndex(songIndex);
882+
if (songIndex < 0) return;
883+
if (play) this.togglePlayIndex(songIndex,true);
878884
else window.$message.success("已添加至下一首播放");
879885
}
880886
/**
881887
* 切换播放索引
882888
* @param index 播放索引
889+
* @param play 是否立即播放
883890
*/
884-
async togglePlayIndex(index: number) {
891+
async togglePlayIndex(index: number,play:boolean = false) {
885892
const dataStore = useDataStore();
886893
const statusStore = useStatusStore();
887894
// 获取数据
888895
const { playList } = dataStore;
889896
// 若超出播放列表
890897
if (index >= playList.length) return;
891898
// 相同
892-
if (statusStore.playIndex === index) {
899+
if (!play && statusStore.playIndex === index) {
893900
this.play();
894901
return;
895902
}
@@ -915,6 +922,8 @@ class Player {
915922
this.cleanPlayList();
916923
return;
917924
}
925+
// 是否为当前播放歌曲
926+
const isCurrentPlay = statusStore.playIndex === index;
918927
// 深拷贝,防止影响原数据
919928
const newPlaylist = cloneDeep(playList);
920929
// 若将移除最后一首
@@ -929,7 +938,7 @@ class Player {
929938
newPlaylist.splice(index, 1);
930939
dataStore.setPlayList(newPlaylist);
931940
// 若为当前播放
932-
if (statusStore.playIndex === index) {
941+
if (isCurrentPlay) {
933942
this.initPlayer(statusStore.playStatus);
934943
}
935944
}
@@ -949,6 +958,7 @@ class Player {
949958
showFullPlayer: false,
950959
playHeartbeatMode: false,
951960
personalFmMode: false,
961+
playIndex: -1,
952962
});
953963
musicStore.resetMusicData();
954964
dataStore.setPlayList([]);

0 commit comments

Comments
 (0)