mirror of
https://github.com/ikun0014/lx-music-mobile.git
synced 2025-07-05 21:58:56 +08:00
修复通知栏更新问题
This commit is contained in:
parent
04ffef4309
commit
6f9480d042
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "lx-music-mobile",
|
"name": "lx-music-mobile",
|
||||||
"version": "0.8.0-beta",
|
"version": "0.8.0-beta1",
|
||||||
"versionCode": 26,
|
"versionCode": 26,
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"ar": "react-native run-android",
|
"ar": "react-native run-android",
|
||||||
|
@ -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 BackgroundTimer from 'react-native-background-timer'
|
||||||
import { defaultUrl } from '@/config'
|
import { defaultUrl } from '@/config'
|
||||||
|
|
||||||
@ -113,8 +113,8 @@ export const playMusic = async(tracks, time) => {
|
|||||||
let musicId = null
|
let musicId = null
|
||||||
let duration = 0
|
let duration = 0
|
||||||
let artwork = null
|
let artwork = null
|
||||||
export const updateMetaInfo = async(track, isPlaying) => {
|
export const updateMetaInfo = async track => {
|
||||||
console.log('+++++updateMusicPic+++++', track.artwork)
|
// console.log('+++++updateMusicPic+++++', track.artwork, track.duration)
|
||||||
|
|
||||||
if (track.musicId == musicId) {
|
if (track.musicId == musicId) {
|
||||||
if (track.artwork != null) artwork = track.artwork
|
if (track.artwork != null) artwork = track.artwork
|
||||||
@ -125,13 +125,14 @@ export const updateMetaInfo = async(track, isPlaying) => {
|
|||||||
duration = track.duration == null ? 0 : track.duration
|
duration = track.duration == null ? 0 : track.duration
|
||||||
}
|
}
|
||||||
|
|
||||||
|
global.isPlaying = await TrackPlayer.getState() == State.Playing
|
||||||
await TrackPlayer.updateNowPlayingMetadata({
|
await TrackPlayer.updateNowPlayingMetadata({
|
||||||
title: track.title || 'Unknow',
|
title: track.title || 'Unknow',
|
||||||
artist: track.artist || 'Unknow',
|
artist: track.artist || 'Unknow',
|
||||||
album: track.album || null,
|
album: track.album || null,
|
||||||
artwork,
|
artwork,
|
||||||
duration,
|
duration,
|
||||||
}, isPlaying)
|
}, global.isPlaying)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -144,8 +145,8 @@ const debounceUpdateMetaInfoTools = {
|
|||||||
let isDelayRun = false
|
let isDelayRun = false
|
||||||
let timer = null
|
let timer = null
|
||||||
let _track = null
|
let _track = null
|
||||||
let _isPlaying = null
|
return track => {
|
||||||
return (track, isPlaying) => {
|
// console.log('debounceUpdateMetaInfoTools', track.duration, track.artwork)
|
||||||
if (timer) {
|
if (timer) {
|
||||||
BackgroundTimer.clearTimeout(timer)
|
BackgroundTimer.clearTimeout(timer)
|
||||||
timer = null
|
timer = null
|
||||||
@ -156,19 +157,16 @@ const debounceUpdateMetaInfoTools = {
|
|||||||
}
|
}
|
||||||
if (isDelayRun) {
|
if (isDelayRun) {
|
||||||
_track = track
|
_track = track
|
||||||
_isPlaying = isPlaying
|
|
||||||
timer = BackgroundTimer.setTimeout(() => {
|
timer = BackgroundTimer.setTimeout(() => {
|
||||||
timer = null
|
timer = null
|
||||||
let track = _track
|
let track = _track
|
||||||
let isPlaying = _isPlaying
|
|
||||||
_track = null
|
_track = null
|
||||||
_isPlaying = null
|
|
||||||
isDelayRun = false
|
isDelayRun = false
|
||||||
fn(track, isPlaying)
|
fn(track)
|
||||||
}, 500)
|
}, 500)
|
||||||
} else {
|
} else {
|
||||||
isDelayRun = true
|
isDelayRun = true
|
||||||
fn(track, isPlaying)
|
fn(track)
|
||||||
delayTimer = BackgroundTimer.setTimeout(() => {
|
delayTimer = BackgroundTimer.setTimeout(() => {
|
||||||
delayTimer = null
|
delayTimer = null
|
||||||
isDelayRun = false
|
isDelayRun = false
|
||||||
@ -177,12 +175,12 @@ const debounceUpdateMetaInfoTools = {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
init() {
|
init() {
|
||||||
return this.debounce((track, isPlaying) => {
|
return this.debounce(track => {
|
||||||
this.track = track
|
this.track = track
|
||||||
return this.updateMetaPromise.then(() => {
|
return this.updateMetaPromise.then(() => {
|
||||||
// console.log('run')
|
// console.log('run')
|
||||||
if (this.track.id === track.id) {
|
if (this.track.id === track.id) {
|
||||||
this.updateMetaPromise = updateMetaInfo(track, isPlaying)
|
this.updateMetaPromise = updateMetaInfo(track)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -87,7 +87,7 @@ export default async() => {
|
|||||||
|
|
||||||
TrackPlayer.addEventListener(TPEvent.PlaybackState, async info => {
|
TrackPlayer.addEventListener(TPEvent.PlaybackState, async info => {
|
||||||
const state = store.getState()
|
const state = store.getState()
|
||||||
console.log('playback-state', TPState[info.state])
|
// console.log('playback-state', TPState[info.state])
|
||||||
|
|
||||||
// console.log((await getCurrentTrack())?.id)
|
// console.log((await getCurrentTrack())?.id)
|
||||||
if (state.player.isGettingUrl) return
|
if (state.player.isGettingUrl) return
|
||||||
@ -105,7 +105,7 @@ export default async() => {
|
|||||||
break
|
break
|
||||||
case TPState.Playing:
|
case TPState.Playing:
|
||||||
retryTrack = null
|
retryTrack = null
|
||||||
console.log('state', 'State.PLAYING')
|
// console.log('state', 'State.PLAYING')
|
||||||
store.dispatch(playerAction.setStatus({ status: STATUS.playing, text: '播放中...' }))
|
store.dispatch(playerAction.setStatus({ status: STATUS.playing, text: '播放中...' }))
|
||||||
TrackPlayer.getPosition().then(position => {
|
TrackPlayer.getPosition().then(position => {
|
||||||
lrcPlay(position * 1000)
|
lrcPlay(position * 1000)
|
||||||
@ -131,7 +131,7 @@ export default async() => {
|
|||||||
break
|
break
|
||||||
case TPState.Buffering:
|
case TPState.Buffering:
|
||||||
store.dispatch(playerAction.setStatus({ status: STATUS.buffering, text: '缓冲中...' }))
|
store.dispatch(playerAction.setStatus({ status: STATUS.buffering, text: '缓冲中...' }))
|
||||||
console.log('state', 'State.BUFFERING')
|
// console.log('state', 'State.BUFFERING')
|
||||||
lrcPause()
|
lrcPause()
|
||||||
break
|
break
|
||||||
case TPState.Connecting:
|
case TPState.Connecting:
|
||||||
@ -155,20 +155,21 @@ export default async() => {
|
|||||||
}
|
}
|
||||||
if (global.isPlayedExit) return handleExitApp()
|
if (global.isPlayedExit) return handleExitApp()
|
||||||
|
|
||||||
|
// console.log('currentIsPlaying', currentIsPlaying, global.isPlaying)
|
||||||
if (currentIsPlaying == global.isPlaying) {
|
if (currentIsPlaying == global.isPlaying) {
|
||||||
const duration = await TrackPlayer.getDuration()
|
const duration = await TrackPlayer.getDuration()
|
||||||
|
// console.log('currentIsPlaying', prevDuration, duration)
|
||||||
if (prevDuration != duration) {
|
if (prevDuration != duration) {
|
||||||
global.isPlaying = currentIsPlaying
|
|
||||||
prevDuration = duration
|
prevDuration = duration
|
||||||
const trackInfo = await getCurrentTrack()
|
const trackInfo = await getCurrentTrack()
|
||||||
if (trackInfo) {
|
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 {
|
} else {
|
||||||
global.isPlaying = currentIsPlaying
|
const [duration, trackInfo] = await Promise.all([TrackPlayer.getDuration(), getCurrentTrack()])
|
||||||
const trackInfo = await getCurrentTrack()
|
prevDuration = duration
|
||||||
delayUpdateMusicInfo(buildTrack({ musicInfo: { ...trackInfo.original }, type: trackInfo.type, url: trackInfo.url, duration: prevDuration }), global.isPlaying)
|
delayUpdateMusicInfo(buildTrack({ musicInfo: { ...trackInfo.original }, type: trackInfo.type, url: trackInfo.url, duration: prevDuration }))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
TrackPlayer.addEventListener(TPEvent.PlaybackTrackChanged, async info => {
|
TrackPlayer.addEventListener(TPEvent.PlaybackTrackChanged, async info => {
|
||||||
|
@ -121,7 +121,7 @@ const handlePlayMusic = async({ getState, dispatch, playMusicInfo, musicInfo, is
|
|||||||
global.isPlaying = false
|
global.isPlaying = false
|
||||||
if (global.restorePlayInfo) {
|
if (global.restorePlayInfo) {
|
||||||
const track = buildTrack({ musicInfo, type })
|
const track = buildTrack({ musicInfo, type })
|
||||||
delayUpdateMusicInfo(track, global.isPlaying)
|
delayUpdateMusicInfo(track)
|
||||||
track.id += track.id + '//restorePlay'
|
track.id += track.id + '//restorePlay'
|
||||||
playMusicId = playMusicId + '//restorePlay'
|
playMusicId = playMusicId + '//restorePlay'
|
||||||
msPlayMusic([track])
|
msPlayMusic([track])
|
||||||
@ -135,7 +135,7 @@ const handlePlayMusic = async({ getState, dispatch, playMusicInfo, musicInfo, is
|
|||||||
if (musicUrl) {
|
if (musicUrl) {
|
||||||
// console.log('+++updateMusicInfo+++')
|
// console.log('+++updateMusicInfo+++')
|
||||||
// setTimeout(() => {
|
// setTimeout(() => {
|
||||||
delayUpdateMusicInfo(buildTrack({ musicInfo, type, url: musicUrl }), global.isPlaying)
|
delayUpdateMusicInfo(buildTrack({ musicInfo, type, url: musicUrl }))
|
||||||
// }, 1000)
|
// }, 1000)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@ -158,7 +158,7 @@ const handlePlayMusic = async({ getState, dispatch, playMusicInfo, musicInfo, is
|
|||||||
status: STATUS.gettingUrl,
|
status: STATUS.gettingUrl,
|
||||||
text: '加载中...',
|
text: '加载中...',
|
||||||
}))
|
}))
|
||||||
delayUpdateMusicInfo(buildTrack({ musicInfo, type }), global.isPlaying)
|
delayUpdateMusicInfo(buildTrack({ musicInfo, type }))
|
||||||
Promise.all([
|
Promise.all([
|
||||||
dispatch(getUrl({ musicInfo, type, isRefresh })),
|
dispatch(getUrl({ musicInfo, type, isRefresh })),
|
||||||
resetPlay(),
|
resetPlay(),
|
||||||
@ -196,7 +196,7 @@ const handlePlayMusic = async({ getState, dispatch, playMusicInfo, musicInfo, is
|
|||||||
if (!musicInfo.img) {
|
if (!musicInfo.img) {
|
||||||
dispatch(getPic(musicInfo)).then(async() => {
|
dispatch(getPic(musicInfo)).then(async() => {
|
||||||
if (playMusicId != id) return
|
if (playMusicId != id) return
|
||||||
delayUpdateMusicInfo(buildTrack({ musicInfo, type }), global.isPlaying)
|
delayUpdateMusicInfo(buildTrack({ musicInfo, type }))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
dispatch(getLrc(musicInfo)).then(({ lyric, tlyric }) => {
|
dispatch(getLrc(musicInfo)).then(({ lyric, tlyric }) => {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user