From 3ea5446fcc3472408e8243a78e092ad3b9424acc Mon Sep 17 00:00:00 2001 From: memorydream <34763046+memorydream@users.noreply.github.com> Date: Wed, 12 Jan 2022 01:04:40 +0800 Subject: [PATCH] fix : PersonalFM can not found next track (#1217) * fix : PersonalFM can not found next track * do not call _loadPersonalFMNextTrack() when reload personal FM * fix: revert * fix: separate playNextTrack * fix: moveToFMTrash * fix * fix: shortcut and _loadPersonalFMNextTrack() * show toast when personal timeout * fix: name --- src/components/FMCard.vue | 7 ++- src/components/Player.vue | 11 +++- src/electron/ipcRenderer.js | 6 +- src/utils/Player.js | 115 ++++++++++++++++++++++++++++-------- src/views/lyrics.vue | 9 ++- 5 files changed, 117 insertions(+), 31 deletions(-) diff --git a/src/components/FMCard.vue b/src/components/FMCard.vue index bb2c866..a485002 100644 --- a/src/components/FMCard.vue +++ b/src/components/FMCard.vue @@ -74,8 +74,11 @@ export default { this.player.playPersonalFM(); }, next() { - this.player.playNextTrack(true); - this.getColor(); + this.player.playNextFMTrack().then(result => { + if (result) { + this.getColor(); + } + }); }, goToAlbum() { if (this.track.album.id === 0) return; diff --git a/src/components/Player.vue b/src/components/Player.vue index 1c1fb14..4ee8c54 100644 --- a/src/components/Player.vue +++ b/src/components/Player.vue @@ -87,9 +87,7 @@ > - @@ -209,6 +207,13 @@ export default { methods: { ...mapMutations(['toggleLyrics']), ...mapActions(['showToast', 'likeATrack']), + playNextTrack() { + if (this.player.isPersonalFM) { + this.player.playNextFMTrack(); + } else { + this.player.playNextTrack(); + } + }, goToNextTracksPage() { if (this.player.isPersonalFM) return; this.$route.name === 'next' diff --git a/src/electron/ipcRenderer.js b/src/electron/ipcRenderer.js index 0222658..4f94b16 100644 --- a/src/electron/ipcRenderer.js +++ b/src/electron/ipcRenderer.js @@ -36,7 +36,11 @@ export function ipcRenderer(vueInstance) { }); ipcRenderer.on('next', () => { - player.playNextTrack(); + if (player.isPersonalFM) { + player.playNextFMTrack(); + } else { + player.playNextTrack(); + } }); ipcRenderer.on('previous', () => { diff --git a/src/utils/Player.js b/src/utils/Player.js index 0b09635..c6d9fda 100644 --- a/src/utils/Player.js +++ b/src/utils/Player.js @@ -14,6 +14,11 @@ const electron = process.env.IS_ELECTRON === true ? window.require('electron') : null; const ipcRenderer = process.env.IS_ELECTRON === true ? electron.ipcRenderer : null; +const excludeSaveKeys = [ + '_playing', + '_personalFMLoading', + '_personalFMNextLoading', +]; export default class { constructor() { @@ -25,6 +30,8 @@ export default class { this._shuffle = false; // true | false this._volume = 1; // 0 to 1 this._volumeBeforeMuted = 1; // 用于保存静音前的音量 + this._personalFMLoading = false; // 是否正在私人FM中加载新的track + this._personalFMNextLoading = false; // 是否正在缓存私人FM的下一首歌曲 // 播放信息 this._list = []; // 播放列表 @@ -162,7 +169,11 @@ export default class { this._setIntervals(); // 初始化私人FM - if (this._personalFMTrack.id === 0 || this._personalFMNextTrack.id === 0) { + if ( + this._personalFMTrack.id === 0 || + this._personalFMNextTrack.id === 0 || + this._personalFMTrack.id === this._personalFMNextTrack.id + ) { personalFM().then(result => { this._personalFMTrack = result.data[0]; this._personalFMNextTrack = result.data[1]; @@ -338,16 +349,22 @@ export default class { return source; } else { store.dispatch('showToast', `无法播放 ${track.name}`); - ifUnplayableThen === 'playNextTrack' - ? this.playNextTrack() - : this.playPrevTrack(); + if (ifUnplayableThen === 'playNextTrack') { + if (this.isPersonalFM) { + this.playNextFMTrack(); + } else { + this.playNextTrack(); + } + } else { + this.playPrevTrack(); + } } }); }); } _cacheNextTrack() { let nextTrackID = this._isPersonalFM - ? this._personalFMNextTrack.id + ? this._personalFMNextTrack?.id ?? 0 : this._getNextTrack()[0]; if (!nextTrackID) return; if (this._personalFMTrack.id == nextTrackID) return; @@ -375,7 +392,11 @@ export default class { this.playPrevTrack(); }); navigator.mediaSession.setActionHandler('nexttrack', () => { - this.playNextTrack(); + if (this.isPersonalFM) { + this.playNextFMTrack(); + } else { + this.playNextTrack(); + } }); navigator.mediaSession.setActionHandler('stop', () => { this.pause(); @@ -428,16 +449,33 @@ export default class { this._scrobble(this._currentTrack, 0, true); if (!this.isPersonalFM && this.repeatMode === 'one') { this._replaceCurrentTrack(this._currentTrack.id); + } else if (this.isPersonalFM) { + this.playNextFMTrack(); } else { this.playNextTrack(); } } _loadPersonalFMNextTrack() { - return personalFM().then(result => { - this._personalFMNextTrack = result.data[0]; - this._cacheNextTrack(); // cache next track - return this._personalFMNextTrack; - }); + if (this._personalFMNextLoading) { + return [false, undefined]; + } + this._personalFMNextLoading = true; + return personalFM() + .then(result => { + if (!result || !result.data) { + this._personalFMNextTrack = undefined; + } else { + this._personalFMNextTrack = result.data[0]; + this._cacheNextTrack(); // cache next track + } + this._personalFMNextLoading = false; + return [true, this._personalFMNextTrack]; + }) + .catch(() => { + this._personalFMNextTrack = undefined; + this._personalFMNextLoading = false; + return [false, this._personalFMNextTrack]; + }); } _playDiscordPresence(track, seekTime = 0) { if ( @@ -467,14 +505,7 @@ export default class { appendTrack(trackID) { this.list.append(trackID); } - playNextTrack(isFM = false) { - if (this._isPersonalFM || isFM === true) { - this._isPersonalFM = true; - this._personalFMTrack = this._personalFMNextTrack; - this._replaceCurrentTrack(this._personalFMTrack.id); - this._loadPersonalFMNextTrack(); - return true; - } + playNextTrack() { // TODO: 切换歌曲时增加加载中的状态 const [trackID, index] = this._getNextTrack(); if (trackID === undefined) { @@ -486,6 +517,34 @@ export default class { this._replaceCurrentTrack(trackID); return true; } + async playNextFMTrack() { + if (this._personalFMLoading) { + return false; + } + + this._isPersonalFM = true; + if (!this._personalFMNextTrack) { + this._personalFMLoading = true; + let result = await personalFM().catch(() => null); + this._personalFMLoading = false; + if (!result || !result.data) { + store.dispatch('showToast', 'personal fm timeout'); + return false; + } + // 这里只能拿到一条数据 + this._personalFMTrack = result.data[0]; + } else { + if (this._personalFMNextTrack.id === this._personalFMTrack.id) { + return false; + } + this._personalFMTrack = this._personalFMNextTrack; + } + if (this._isPersonalFM) { + this._replaceCurrentTrack(this._personalFMTrack.id); + } + this._loadPersonalFMNextTrack(); + return true; + } playPrevTrack() { const [trackID, index] = this._getPrevTrack(); if (trackID === undefined) return false; @@ -496,7 +555,7 @@ export default class { saveSelfToLocalStorage() { let player = {}; for (let [key, value] of Object.entries(this)) { - if (key === '_playing') continue; + if (excludeSaveKeys.includes(key)) continue; player[key] = value; } @@ -620,7 +679,13 @@ export default class { } addTrackToPlayNext(trackID, playNow = false) { this._playNextList.push(trackID); - if (playNow) this.playNextTrack(); + if (playNow) { + if (this.isPersonalFM) { + this.playNextFMTrack(); + } else { + this.playNextTrack(); + } + } } playPersonalFM() { this._isPersonalFM = true; @@ -633,10 +698,12 @@ export default class { this.playOrPause(); } } - moveToFMTrash() { + async moveToFMTrash() { this._isPersonalFM = true; - this.playNextTrack(); - fmTrash(this._personalFMTrack.id); + let id = this._personalFMTrack.id; + if (await this.playNextFMTrack()) { + fmTrash(id); + } } sendSelfToIpcMain() { diff --git a/src/views/lyrics.vue b/src/views/lyrics.vue index 76602b9..7cf9817 100644 --- a/src/views/lyrics.vue +++ b/src/views/lyrics.vue @@ -151,7 +151,7 @@ @@ -318,6 +318,13 @@ export default { methods: { ...mapMutations(['toggleLyrics']), ...mapActions(['likeATrack']), + playNextTrack() { + if (this.player.isPersonalFM) { + this.player.playNextFMTrack(); + } else { + this.player.playNextTrack(); + } + }, getLyric() { if (!this.currentTrack.id) return; return getLyric(this.currentTrack.id).then(data => {