diff --git a/package.json b/package.json index 1898ebda..9d43cba2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "lx-music-mobile", - "version": "0.8.0-beta", + "version": "0.8.0-beta1", "versionCode": 26, "scripts": { "ar": "react-native run-android", diff --git a/src/plugins/player/playList.js b/src/plugins/player/playList.js index 96404679..6c2390f7 100644 --- a/src/plugins/player/playList.js +++ b/src/plugins/player/playList.js @@ -1,4 +1,4 @@ -import TrackPlayer from 'react-native-track-player' +import TrackPlayer, { State } from 'react-native-track-player' import BackgroundTimer from 'react-native-background-timer' import { defaultUrl } from '@/config' @@ -113,8 +113,8 @@ export const playMusic = async(tracks, time) => { let musicId = null let duration = 0 let artwork = null -export const updateMetaInfo = async(track, isPlaying) => { - console.log('+++++updateMusicPic+++++', track.artwork) +export const updateMetaInfo = async track => { + // console.log('+++++updateMusicPic+++++', track.artwork, track.duration) if (track.musicId == musicId) { if (track.artwork != null) artwork = track.artwork @@ -125,13 +125,14 @@ export const updateMetaInfo = async(track, isPlaying) => { duration = track.duration == null ? 0 : track.duration } + global.isPlaying = await TrackPlayer.getState() == State.Playing await TrackPlayer.updateNowPlayingMetadata({ title: track.title || 'Unknow', artist: track.artist || 'Unknow', album: track.album || null, artwork, duration, - }, isPlaying) + }, global.isPlaying) } @@ -144,8 +145,8 @@ const debounceUpdateMetaInfoTools = { let isDelayRun = false let timer = null let _track = null - let _isPlaying = null - return (track, isPlaying) => { + return track => { + // console.log('debounceUpdateMetaInfoTools', track.duration, track.artwork) if (timer) { BackgroundTimer.clearTimeout(timer) timer = null @@ -156,19 +157,16 @@ const debounceUpdateMetaInfoTools = { } if (isDelayRun) { _track = track - _isPlaying = isPlaying timer = BackgroundTimer.setTimeout(() => { timer = null let track = _track - let isPlaying = _isPlaying _track = null - _isPlaying = null isDelayRun = false - fn(track, isPlaying) + fn(track) }, 500) } else { isDelayRun = true - fn(track, isPlaying) + fn(track) delayTimer = BackgroundTimer.setTimeout(() => { delayTimer = null isDelayRun = false @@ -177,12 +175,12 @@ const debounceUpdateMetaInfoTools = { } }, init() { - return this.debounce((track, isPlaying) => { + return this.debounce(track => { this.track = track return this.updateMetaPromise.then(() => { // console.log('run') if (this.track.id === track.id) { - this.updateMetaPromise = updateMetaInfo(track, isPlaying) + this.updateMetaPromise = updateMetaInfo(track) } }) }) diff --git a/src/plugins/player/service.js b/src/plugins/player/service.js index 8e4314cf..5910bb2c 100644 --- a/src/plugins/player/service.js +++ b/src/plugins/player/service.js @@ -87,7 +87,7 @@ export default async() => { TrackPlayer.addEventListener(TPEvent.PlaybackState, async info => { const state = store.getState() - console.log('playback-state', TPState[info.state]) + // console.log('playback-state', TPState[info.state]) // console.log((await getCurrentTrack())?.id) if (state.player.isGettingUrl) return @@ -105,7 +105,7 @@ export default async() => { break case TPState.Playing: retryTrack = null - console.log('state', 'State.PLAYING') + // console.log('state', 'State.PLAYING') store.dispatch(playerAction.setStatus({ status: STATUS.playing, text: '播放中...' })) TrackPlayer.getPosition().then(position => { lrcPlay(position * 1000) @@ -131,7 +131,7 @@ export default async() => { break case TPState.Buffering: store.dispatch(playerAction.setStatus({ status: STATUS.buffering, text: '缓冲中...' })) - console.log('state', 'State.BUFFERING') + // console.log('state', 'State.BUFFERING') lrcPause() break case TPState.Connecting: @@ -155,20 +155,21 @@ export default async() => { } if (global.isPlayedExit) return handleExitApp() + // console.log('currentIsPlaying', currentIsPlaying, global.isPlaying) if (currentIsPlaying == global.isPlaying) { const duration = await TrackPlayer.getDuration() + // console.log('currentIsPlaying', prevDuration, duration) if (prevDuration != duration) { - global.isPlaying = currentIsPlaying prevDuration = duration const trackInfo = await getCurrentTrack() if (trackInfo) { - delayUpdateMusicInfo(buildTrack({ musicInfo: { ...trackInfo.original }, type: trackInfo.type, url: trackInfo.url, duration }), global.isPlaying) + delayUpdateMusicInfo(buildTrack({ musicInfo: { ...trackInfo.original }, type: trackInfo.type, url: trackInfo.url, duration })) } } } else { - global.isPlaying = currentIsPlaying - const trackInfo = await getCurrentTrack() - delayUpdateMusicInfo(buildTrack({ musicInfo: { ...trackInfo.original }, type: trackInfo.type, url: trackInfo.url, duration: prevDuration }), global.isPlaying) + const [duration, trackInfo] = await Promise.all([TrackPlayer.getDuration(), getCurrentTrack()]) + prevDuration = duration + delayUpdateMusicInfo(buildTrack({ musicInfo: { ...trackInfo.original }, type: trackInfo.type, url: trackInfo.url, duration: prevDuration })) } }) TrackPlayer.addEventListener(TPEvent.PlaybackTrackChanged, async info => { diff --git a/src/store/modules/player/action.js b/src/store/modules/player/action.js index ca71969c..b4e3ded3 100644 --- a/src/store/modules/player/action.js +++ b/src/store/modules/player/action.js @@ -121,7 +121,7 @@ const handlePlayMusic = async({ getState, dispatch, playMusicInfo, musicInfo, is global.isPlaying = false if (global.restorePlayInfo) { const track = buildTrack({ musicInfo, type }) - delayUpdateMusicInfo(track, global.isPlaying) + delayUpdateMusicInfo(track) track.id += track.id + '//restorePlay' playMusicId = playMusicId + '//restorePlay' msPlayMusic([track]) @@ -135,7 +135,7 @@ const handlePlayMusic = async({ getState, dispatch, playMusicInfo, musicInfo, is if (musicUrl) { // console.log('+++updateMusicInfo+++') // setTimeout(() => { - delayUpdateMusicInfo(buildTrack({ musicInfo, type, url: musicUrl }), global.isPlaying) + delayUpdateMusicInfo(buildTrack({ musicInfo, type, url: musicUrl })) // }, 1000) } }) @@ -158,7 +158,7 @@ const handlePlayMusic = async({ getState, dispatch, playMusicInfo, musicInfo, is status: STATUS.gettingUrl, text: '加载中...', })) - delayUpdateMusicInfo(buildTrack({ musicInfo, type }), global.isPlaying) + delayUpdateMusicInfo(buildTrack({ musicInfo, type })) Promise.all([ dispatch(getUrl({ musicInfo, type, isRefresh })), resetPlay(), @@ -196,7 +196,7 @@ const handlePlayMusic = async({ getState, dispatch, playMusicInfo, musicInfo, is if (!musicInfo.img) { dispatch(getPic(musicInfo)).then(async() => { if (playMusicId != id) return - delayUpdateMusicInfo(buildTrack({ musicInfo, type }), global.isPlaying) + delayUpdateMusicInfo(buildTrack({ musicInfo, type })) }) } dispatch(getLrc(musicInfo)).then(({ lyric, tlyric }) => {