fix(music-player): 修复音乐播放和歌词显示问题
-优化歌词解析逻辑,支持不同格式的歌词时间戳 - 修复设置当前音乐时的音频加载和播放问题 - 优化更新音乐 URL 的逻辑,确保平滑切换音源 - 修复了一些与音乐播放相关的潜在错误和不良行为
This commit is contained in:
@@ -44,33 +44,43 @@ export const usePlayerStore = defineStore('player', {
|
||||
|
||||
// 设置当前音乐
|
||||
async setCurrentMusic(music) {
|
||||
if (!music) return
|
||||
|
||||
this.initAudio()
|
||||
this.currentMusic = music
|
||||
|
||||
// 检查播放列表中是否已存在该音乐
|
||||
const index = this.playlist.findIndex(item => item.id === music.id)
|
||||
if (index !== -1) {
|
||||
this.currentIndex = index
|
||||
} else {
|
||||
this.addToPlaylist(music)
|
||||
this.currentIndex = this.playlist.length - 1
|
||||
if (!music || (this.currentMusic && this.currentMusic.id === music.id && this.isPlaying)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 设置音频源并播放
|
||||
if (music.url) {
|
||||
this.audio.src = music.url
|
||||
this.play()
|
||||
this.initAudio();
|
||||
|
||||
// 在加载新音乐之前暂停并重置当前音频
|
||||
if (this.audio) {
|
||||
this.pause();
|
||||
this.audio.src = ''; // 清空旧的音频源
|
||||
}
|
||||
|
||||
this.currentMusic = { ...music };
|
||||
this.isPlaying = false;
|
||||
this.currentTime = 0;
|
||||
this.duration = 0;
|
||||
|
||||
const index = this.playlist.findIndex(item => item.id === music.id);
|
||||
if (index !== -1) {
|
||||
this.currentIndex = index;
|
||||
} else {
|
||||
this.addToPlaylist(music);
|
||||
this.currentIndex = this.playlist.length - 1;
|
||||
}
|
||||
|
||||
if (music.url) {
|
||||
this.audio.src = music.url;
|
||||
this.audio.load(); // 明确加载新音频
|
||||
this.play();
|
||||
|
||||
// 调用播放API,记录播放次数
|
||||
try {
|
||||
await playMusic(music.id)
|
||||
await playMusic(music.id);
|
||||
} catch (error) {
|
||||
console.error('记录播放失败:', error)
|
||||
console.error('记录播放失败:', error);
|
||||
}
|
||||
} else {
|
||||
console.error('音乐URL不存在')
|
||||
console.error('音乐URL不存在');
|
||||
}
|
||||
},
|
||||
|
||||
@@ -253,18 +263,17 @@ export const usePlayerStore = defineStore('player', {
|
||||
|
||||
// 更新音乐URL
|
||||
updateMusicUrl(id, url) {
|
||||
// 更新当前音乐的URL
|
||||
if (this.currentMusic && this.currentMusic.id === id) {
|
||||
this.currentMusic.url = url
|
||||
if (this.audio) {
|
||||
this.audio.src = url
|
||||
}
|
||||
const musicInPlaylist = this.playlist.find(item => item.id === id);
|
||||
if (musicInPlaylist) {
|
||||
musicInPlaylist.url = url;
|
||||
}
|
||||
|
||||
// 更新播放列表中的URL
|
||||
const index = this.playlist.findIndex(item => item.id === id)
|
||||
if (index !== -1) {
|
||||
this.playlist[index].url = url
|
||||
if (this.currentMusic && this.currentMusic.id === id) {
|
||||
this.currentMusic.url = url;
|
||||
// 如果是当前播放的音乐,则重新设置音频源并播放
|
||||
if (this.audio && this.audio.src !== url) {
|
||||
this.setCurrentMusic({ ...this.currentMusic, url });
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
|
||||
Reference in New Issue
Block a user