修复通知栏更新问题

This commit is contained in:
lyswhut 2021-09-01 23:08:58 +08:00
parent 04ffef4309
commit 6f9480d042
4 changed files with 25 additions and 26 deletions

View File

@ -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",

View File

@ -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)
}
})
})

View File

@ -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 => {

View File

@ -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 }) => {