修复停止播放后的播放器状态清理问题

This commit is contained in:
lyswhut 2022-06-16 11:18:56 +08:00
parent 02a8d8422f
commit ee623f74bb
7 changed files with 30 additions and 15 deletions

View File

@ -87,14 +87,15 @@ public class Lyric extends LyricPlayer {
private void setViewLyric(int lineNum) { private void setViewLyric(int lineNum) {
lastLine = lineNum; lastLine = lineNum;
if (lyricView == null) return; if (lyricView == null) return;
if (lineNum < 0 || lineNum > lines.size() - 1) return; if (lineNum >= 0 && lineNum < lines.size()) {
HashMap line = (HashMap) lines.get(lineNum); HashMap line = (HashMap) lines.get(lineNum);
if (line == null) { if (line != null) {
lyricView.setLyric("", new ArrayList<>(0));
} else {
lyricView.setLyric((String) line.get("text"), (ArrayList<String>) line.get("extendedLyrics")); lyricView.setLyric((String) line.get("text"), (ArrayList<String>) line.get("extendedLyrics"));
return;
} }
} }
lyricView.setLyric("", new ArrayList<>(0));
}
public void showLyric(Bundle options, Promise promise) { public void showLyric(Bundle options, Promise promise) {
if (lyricEvent == null) lyricEvent = new LyricEvent(reactAppContext); if (lyricEvent == null) lyricEvent = new LyricEvent(reactAppContext);
@ -136,6 +137,7 @@ public class Lyric extends LyricPlayer {
@Override @Override
public void onSetLyric(List lines) { public void onSetLyric(List lines) {
this.lines = lines; this.lines = lines;
setViewLyric(-1);
// for (int i = 0; i < lines.size(); i++) { // for (int i = 0; i < lines.size(); i++) {
// HashMap line = (HashMap) lines.get(i); // HashMap line = (HashMap) lines.get(i);
// Log.d("Lyric", "onSetLyric: " +(String) line.get("text") + " " + line.get("extendedLyrics")); // Log.d("Lyric", "onSetLyric: " +(String) line.get("text") + " " + line.get("extendedLyrics"));

View File

@ -15,3 +15,4 @@
- 修复桌面歌词转繁体设置不立即生效的问题 - 修复桌面歌词转繁体设置不立即生效的问题
- 修复搜索、歌单、排行榜列表可能在切换新内容后出现上次列表内容的残留问题(#118 - 修复搜索、歌单、排行榜列表可能在切换新内容后出现上次列表内容的残留问题(#118
- 修复在某些系统上播放音乐会导致应用崩溃的问题(#129 - 修复在某些系统上播放音乐会导致应用崩溃的问题(#129
- 修复停止播放后的播放器状态清理问题

View File

@ -32,8 +32,8 @@ const lrcTools = {
}, },
onSetLyric(lines) { onSetLyric(lines) {
this.currentLines = lines this.currentLines = lines
for (const hook of this.setLyricHooks) hook(lines)
for (const hook of this.playHooks) hook(-1, '') for (const hook of this.playHooks) hook(-1, '')
for (const hook of this.setLyricHooks) hook(lines)
}, },
addPlayHook(callback) { addPlayHook(callback) {
this.playHooks.push(callback) this.playHooks.push(callback)

View File

@ -135,7 +135,7 @@ export const updateMetaInfo = async track => {
// artwork = global.playInfo.musicInfo.img // artwork = global.playInfo.musicInfo.img
// duration = global.playInfo.duration || 0 // duration = global.playInfo.duration || 0
// } // }
console.log(global.playInfo)
global.playInfo.isPlaying = await TrackPlayer.getState() == State.Playing global.playInfo.isPlaying = await TrackPlayer.getState() == State.Playing
await TrackPlayer.updateNowPlayingMetadata({ await TrackPlayer.updateNowPlayingMetadata({
title: track.title || 'Unknow', title: track.title || 'Unknow',
@ -143,7 +143,7 @@ export const updateMetaInfo = async track => {
album: track.album || null, album: track.album || null,
artwork: isShowNotificationImage ? global.playInfo?.currentPlayMusicInfo?.img ?? null : null, artwork: isShowNotificationImage ? global.playInfo?.currentPlayMusicInfo?.img ?? null : null,
duration: global.playInfo?.duration || 0, duration: global.playInfo?.duration || 0,
}, global.playInfo.isPlaying) }, global.playInfo?.isPlaying)
} }

View File

@ -20,7 +20,7 @@ import {
import { getRandom } from '@/utils' import { getRandom } from '@/utils'
import { getMusicUrl, saveMusicUrl, getLyric, saveLyric, assertApiSupport, savePlayInfo, saveList, checkNotificationPermission } from '@/utils/tools' import { getMusicUrl, saveMusicUrl, getLyric, saveLyric, assertApiSupport, savePlayInfo, saveList, checkNotificationPermission } from '@/utils/tools'
import { playInfo as playInfoGetter } from './getter' import { playInfo as playInfoGetter } from './getter'
import { play as lrcPlay, setLyric, pause as lrcPause, toggleTranslation as lrcToggleTranslation, toggleRoma as lrcToggleRoma } from '@/utils/lyric' import { play as lrcPlay, setLyric, pause as lrcPause, stop as lrcStop, toggleTranslation as lrcToggleTranslation, toggleRoma as lrcToggleRoma } from '@/utils/lyric'
import { import {
showLyric, hideLyric, showLyric, hideLyric,
setLyric as lrcdSetLyric, setLyric as lrcdSetLyric,
@ -127,7 +127,7 @@ const handlePlayMusic = async({ getState, dispatch, playMusicInfo, musicInfo, is
clearTimeout(timeout) clearTimeout(timeout)
timeout = null timeout = null
} }
setLyric('') // setLyric('')
console.log('Handle Play Music ====================>', musicInfo.name) console.log('Handle Play Music ====================>', musicInfo.name)
global.playInfo.currentPlayMusicInfo = _playMusicInfo = musicInfo global.playInfo.currentPlayMusicInfo = _playMusicInfo = musicInfo
let id = `${musicInfo.source}//${musicInfo.songmid}//${type}` let id = `${musicInfo.source}//${musicInfo.songmid}//${type}`
@ -304,9 +304,11 @@ const getNextMusicInfo = state => {
*/ */
export const stopMusic = () => async(dispatch, getState) => { export const stopMusic = () => async(dispatch, getState) => {
_playMusicInfo = null _playMusicInfo = null
dispatch(setPlayIndex(-1)) await dispatch(playMusic(null))
dispatch(setStatus({ status: STATUS.stop, text: '' })) dispatch(setStatus({ status: STATUS.stop, text: '' }))
await stop() lrcStop()
savePlayInfo(null)
delayUpdateMusicInfo({})
} }
export const pauseMusic = () => async(dispatch, getState) => { export const pauseMusic = () => async(dispatch, getState) => {
@ -481,6 +483,9 @@ export const playMusic = playMusicInfo => async(dispatch, getState) => {
playIndex, playIndex,
}, },
}) })
global.playInfo.currentPlayMusicInfo = _playMusicInfo = playMusicInfo
playMusicId = ''
global.playInfo.isPlaying = false
await stop() await stop()
} else { // 设置歌曲信息并播放歌曲 } else { // 设置歌曲信息并播放歌曲
setLyric('') setLyric('')

View File

@ -119,8 +119,8 @@ const mutations = {
[TYPES.setPlayMusicInfo](state, { playMusicInfo, playIndex }) { [TYPES.setPlayMusicInfo](state, { playMusicInfo, playIndex }) {
return { return {
...state, ...state,
playMusicInfo: playMusicInfo, playMusicInfo,
playIndex: playIndex, playIndex,
} }
}, },
} }

View File

@ -49,6 +49,13 @@ export const pause = () => {
lrcdPause() lrcdPause()
} }
/**
* stop lyric
*/
export const stop = () => {
setLyric('')
}
/** /**
* toggle show translation * toggle show translation
* @param {Boolean} isShowTranslation is show translation * @param {Boolean} isShowTranslation is show translation