mirror of
https://github.com/ultrasonic/ultrasonic
synced 2025-02-11 09:10:57 +01:00
Merged player state and track changes into a single event
This commit is contained in:
parent
66df5b1daf
commit
69825b28bb
@ -185,7 +185,7 @@ class LocalMediaPlayer : KoinComponent {
|
|||||||
// In some cases this function is called twice
|
// In some cases this function is called twice
|
||||||
if (this.currentPlaying == currentPlaying) return
|
if (this.currentPlaying == currentPlaying) return
|
||||||
this.currentPlaying = currentPlaying
|
this.currentPlaying = currentPlaying
|
||||||
RxBus.currentPlayingPublisher.onNext(RxBus.StateWithTrack(playerState, currentPlaying))
|
RxBus.playerStatePublisher.onNext(RxBus.StateWithTrack(playerState, currentPlaying))
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -70,6 +70,9 @@ class MediaPlayerService : Service() {
|
|||||||
private var notificationBuilder: NotificationCompat.Builder? = null
|
private var notificationBuilder: NotificationCompat.Builder? = null
|
||||||
private var rxBusSubscription: CompositeDisposable = CompositeDisposable()
|
private var rxBusSubscription: CompositeDisposable = CompositeDisposable()
|
||||||
|
|
||||||
|
private var currentPlayerState: PlayerState? = null
|
||||||
|
private var currentTrack: DownloadFile? = null
|
||||||
|
|
||||||
override fun onBind(intent: Intent): IBinder {
|
override fun onBind(intent: Intent): IBinder {
|
||||||
return binder
|
return binder
|
||||||
}
|
}
|
||||||
@ -105,10 +108,6 @@ class MediaPlayerService : Service() {
|
|||||||
playerStateChangedHandler(it.state, it.track)
|
playerStateChangedHandler(it.state, it.track)
|
||||||
}
|
}
|
||||||
|
|
||||||
rxBusSubscription += RxBus.currentPlayingObservable.subscribe {
|
|
||||||
currentPlayingChangedHandler(it.state, it.track)
|
|
||||||
}
|
|
||||||
|
|
||||||
rxBusSubscription += RxBus.mediaSessionTokenObservable.subscribe {
|
rxBusSubscription += RxBus.mediaSessionTokenObservable.subscribe {
|
||||||
mediaSessionToken = it
|
mediaSessionToken = it
|
||||||
}
|
}
|
||||||
@ -349,44 +348,15 @@ class MediaPlayerService : Service() {
|
|||||||
UltrasonicAppWidgetProvider4X4.getInstance().notifyChange(context, song, started, false)
|
UltrasonicAppWidgetProvider4X4.getInstance().notifyChange(context, song, started, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun currentPlayingChangedHandler(
|
|
||||||
playerState: PlayerState,
|
|
||||||
currentPlaying: DownloadFile?
|
|
||||||
) {
|
|
||||||
Util.broadcastNewTrackInfo(this@MediaPlayerService, currentPlaying?.song)
|
|
||||||
Util.broadcastA2dpMetaDataChange(
|
|
||||||
this@MediaPlayerService, playerPosition, currentPlaying,
|
|
||||||
downloader.all.size, downloader.currentPlayingIndex + 1
|
|
||||||
)
|
|
||||||
|
|
||||||
// Update widget
|
|
||||||
val song = currentPlaying?.song
|
|
||||||
|
|
||||||
updateWidget(playerState, song)
|
|
||||||
|
|
||||||
if (currentPlaying != null) {
|
|
||||||
updateNotification(playerState, currentPlaying)
|
|
||||||
} else {
|
|
||||||
stopForeground(true)
|
|
||||||
isInForeground = false
|
|
||||||
stopIfIdle()
|
|
||||||
}
|
|
||||||
|
|
||||||
Timber.d("Processed currently playing track change")
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun playerStateChangedHandler(
|
private fun playerStateChangedHandler(
|
||||||
playerState: PlayerState,
|
playerState: PlayerState,
|
||||||
currentPlaying: DownloadFile?
|
currentPlaying: DownloadFile?
|
||||||
) {
|
) {
|
||||||
|
|
||||||
val context = this@MediaPlayerService
|
val context = this@MediaPlayerService
|
||||||
|
// AVRCP handles these separately so we must differentiate between the cases
|
||||||
if (playerState === PlayerState.PAUSED) {
|
val isStateChanged = playerState != currentPlayerState
|
||||||
playbackStateSerializer.serialize(
|
val isTrackChanged = currentPlaying != currentTrack
|
||||||
downloader.playlist, downloader.currentPlayingIndex, playerPosition
|
if (!isStateChanged && !isTrackChanged) return
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
val showWhenPaused = playerState !== PlayerState.STOPPED &&
|
val showWhenPaused = playerState !== PlayerState.STOPPED &&
|
||||||
Settings.isNotificationAlwaysEnabled
|
Settings.isNotificationAlwaysEnabled
|
||||||
@ -394,12 +364,38 @@ class MediaPlayerService : Service() {
|
|||||||
val show = playerState === PlayerState.STARTED || showWhenPaused
|
val show = playerState === PlayerState.STARTED || showWhenPaused
|
||||||
val song = currentPlaying?.song
|
val song = currentPlaying?.song
|
||||||
|
|
||||||
Util.broadcastPlaybackStatusChange(context, playerState)
|
if (isStateChanged) {
|
||||||
Util.broadcastA2dpPlayStatusChange(
|
when {
|
||||||
context, playerState, song,
|
playerState === PlayerState.PAUSED -> {
|
||||||
downloader.playlist.size,
|
playbackStateSerializer.serialize(
|
||||||
downloader.playlist.indexOf(currentPlaying) + 1, playerPosition
|
downloader.playlist, downloader.currentPlayingIndex, playerPosition
|
||||||
)
|
)
|
||||||
|
}
|
||||||
|
playerState === PlayerState.STARTED -> {
|
||||||
|
scrobbler.scrobble(currentPlaying, false)
|
||||||
|
}
|
||||||
|
playerState === PlayerState.COMPLETED -> {
|
||||||
|
scrobbler.scrobble(currentPlaying, true)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Util.broadcastPlaybackStatusChange(context, playerState)
|
||||||
|
Util.broadcastA2dpPlayStatusChange(
|
||||||
|
context, playerState, song,
|
||||||
|
downloader.playlist.size,
|
||||||
|
downloader.playlist.indexOf(currentPlaying) + 1, playerPosition
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
// State didn't change, only the track
|
||||||
|
Util.broadcastA2dpMetaDataChange(
|
||||||
|
this@MediaPlayerService, playerPosition, currentPlaying,
|
||||||
|
downloader.all.size, downloader.currentPlayingIndex + 1
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isTrackChanged) {
|
||||||
|
Util.broadcastNewTrackInfo(this@MediaPlayerService, currentPlaying?.song)
|
||||||
|
}
|
||||||
|
|
||||||
// Update widget
|
// Update widget
|
||||||
updateWidget(playerState, song)
|
updateWidget(playerState, song)
|
||||||
@ -415,11 +411,9 @@ class MediaPlayerService : Service() {
|
|||||||
stopIfIdle()
|
stopIfIdle()
|
||||||
}
|
}
|
||||||
|
|
||||||
if (playerState === PlayerState.STARTED) {
|
currentPlayerState = playerState
|
||||||
scrobbler.scrobble(currentPlaying, false)
|
currentTrack = currentPlaying
|
||||||
} else if (playerState === PlayerState.COMPLETED) {
|
|
||||||
scrobbler.scrobble(currentPlaying, true)
|
|
||||||
}
|
|
||||||
Timber.d("Processed player state change")
|
Timber.d("Processed player state change")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -37,13 +37,6 @@ class RxBus {
|
|||||||
.replay(1)
|
.replay(1)
|
||||||
.autoConnect(0)
|
.autoConnect(0)
|
||||||
|
|
||||||
val currentPlayingPublisher: PublishSubject<StateWithTrack> =
|
|
||||||
PublishSubject.create()
|
|
||||||
val currentPlayingObservable: Observable<StateWithTrack> =
|
|
||||||
currentPlayingPublisher.observeOn(AndroidSchedulers.mainThread())
|
|
||||||
.replay(1)
|
|
||||||
.autoConnect(0)
|
|
||||||
|
|
||||||
val playlistPublisher: PublishSubject<List<DownloadFile>> =
|
val playlistPublisher: PublishSubject<List<DownloadFile>> =
|
||||||
PublishSubject.create()
|
PublishSubject.create()
|
||||||
val playlistObservable: Observable<List<DownloadFile>> =
|
val playlistObservable: Observable<List<DownloadFile>> =
|
||||||
|
Loading…
x
Reference in New Issue
Block a user