Remove legacyPlayerState

This commit is contained in:
tzugen 2022-04-18 09:26:13 +02:00
parent 647435fe55
commit 6115ac995f
No known key found for this signature in database
GPG Key ID: 61E9C34BC10EC930
6 changed files with 41 additions and 72 deletions

View File

@ -18,6 +18,8 @@
*/
package org.moire.ultrasonic.view;
import static org.koin.java.KoinJavaComponent.inject;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
@ -29,14 +31,11 @@ import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.Observer;
import org.moire.ultrasonic.audiofx.VisualizerController;
import org.moire.ultrasonic.domain.PlayerState;
import org.moire.ultrasonic.service.MediaPlayerController;
import kotlin.Lazy;
import timber.log.Timber;
import static org.koin.java.KoinJavaComponent.inject;
/**
* A simple class that draws waveform data received from a
* {@link Visualizer.OnDataCaptureListener#onWaveFormDataCapture}
@ -130,7 +129,7 @@ public class VisualizerView extends View
return;
}
if (mediaPlayerControllerLazy.getValue().getLegacyPlayerState() != PlayerState.STARTED)
if (!mediaPlayerControllerLazy.getValue().isPlaying())
{
return;
}

View File

@ -46,7 +46,6 @@ import org.koin.androidx.viewmodel.ext.android.viewModel
import org.moire.ultrasonic.R
import org.moire.ultrasonic.data.ActiveServerProvider
import org.moire.ultrasonic.data.ServerSettingDao
import org.moire.ultrasonic.domain.PlayerState
import org.moire.ultrasonic.fragment.OnBackPressedHandler
import org.moire.ultrasonic.model.ServerSettingsModel
import org.moire.ultrasonic.provider.SearchSuggestionProvider
@ -183,7 +182,7 @@ class NavigationActivity : AppCompatActivity() {
}
rxBusSubscription += RxBus.playerStateObservable.subscribe {
if (it.state === PlayerState.STARTED || it.state === PlayerState.PAUSED)
if (it.state == STATE_READY)
showNowPlaying()
else
hideNowPlaying()

View File

@ -109,6 +109,7 @@ class TrackViewHolder(val view: View) : RecyclerView.ViewHolder(view), Checkable
}
rxSubscription = RxBus.playerStateObservable.subscribe {
Timber.i("NEW PLAY STATE")
setPlayIcon(it.index == bindingAdapterPosition && it.track == downloadFile)
}
}

View File

@ -22,7 +22,6 @@ import java.lang.Exception
import kotlin.math.abs
import org.koin.android.ext.android.inject
import org.moire.ultrasonic.R
import org.moire.ultrasonic.domain.PlayerState
import org.moire.ultrasonic.service.MediaPlayerController
import org.moire.ultrasonic.service.RxBus
import org.moire.ultrasonic.subsonic.ImageLoaderProvider
@ -85,20 +84,18 @@ class NowPlayingFragment : Fragment() {
@SuppressLint("ClickableViewAccessibility")
private fun update() {
try {
val playerState = mediaPlayerController.legacyPlayerState
if (playerState === PlayerState.PAUSED) {
playButton!!.setImageDrawable(
getDrawableFromAttribute(
requireContext(), R.attr.media_play
)
)
} else if (playerState === PlayerState.STARTED) {
if (mediaPlayerController.isPlaying) {
playButton!!.setImageDrawable(
getDrawableFromAttribute(
requireContext(), R.attr.media_pause
)
)
} else {
playButton!!.setImageDrawable(
getDrawableFromAttribute(
requireContext(), R.attr.media_play
)
)
}
val file = mediaPlayerController.currentPlayingLegacy

View File

@ -13,7 +13,6 @@ import androidx.core.net.toUri
import androidx.media3.common.MediaItem
import androidx.media3.common.MediaMetadata
import androidx.media3.common.Player
import androidx.media3.common.Player.STATE_BUFFERING
import androidx.media3.common.Timeline
import androidx.media3.session.MediaController
import androidx.media3.session.SessionToken
@ -91,13 +90,11 @@ class MediaPlayerController(
}
override fun onPlaybackStateChanged(playbackState: Int) {
translatePlaybackState(playbackState = playbackState)
playerStateChangedHandler()
publishPlaybackState()
}
override fun onIsPlayingChanged(isPlaying: Boolean) {
translatePlaybackState(isPlaying = isPlaying)
playerStateChangedHandler()
publishPlaybackState()
}
@ -121,57 +118,29 @@ class MediaPlayerController(
Timber.i("MediaPlayerController created")
}
@Suppress("DEPRECATION")
fun translatePlaybackState(
playbackState: Int = controller?.playbackState ?: 0,
isPlaying: Boolean = controller?.isPlaying ?: false
) {
legacyPlayerState = when (playbackState) {
STATE_BUFFERING -> PlayerState.DOWNLOADING
Player.STATE_ENDED -> {
PlayerState.COMPLETED
}
Player.STATE_IDLE -> {
PlayerState.IDLE
}
Player.STATE_READY -> {
if (isPlaying) {
PlayerState.STARTED
} else {
PlayerState.PAUSED
}
}
else -> {
PlayerState.IDLE
}
}
}
private fun playerStateChangedHandler() {
val playerState = legacyPlayerState
val currentPlaying = legacyPlaylistManager.currentPlaying
when {
playerState === PlayerState.PAUSED -> {
playbackStateSerializer.serialize(
playList, currentMediaItemIndex, playerPosition
)
when (playbackState) {
Player.STATE_READY -> {
if (isPlaying) {
scrobbler.scrobble(currentPlaying, false)
} else {
playbackStateSerializer.serialize(
playList, currentMediaItemIndex, playerPosition
)
}
}
playerState === PlayerState.STARTED -> {
scrobbler.scrobble(currentPlaying, false)
}
playerState === PlayerState.COMPLETED -> {
Player.STATE_ENDED -> {
scrobbler.scrobble(currentPlaying, true)
}
}
// Update widget
if (currentPlaying != null) {
updateWidget(playerState, currentPlaying.track)
updateWidget(currentPlaying.track)
}
Timber.d("Processed player state change")
}
private fun onTrackCompleted() {
@ -190,23 +159,23 @@ class MediaPlayerController(
}
private fun publishPlaybackState() {
RxBus.playerStatePublisher.onNext(
RxBus.StateWithTrack(
state = legacyPlayerState,
track = legacyPlaylistManager.currentPlaying,
index = currentMediaItemIndex
)
val newState = RxBus.StateWithTrack(
track = legacyPlaylistManager.currentPlaying,
index = currentMediaItemIndex,
isPlaying = isPlaying,
state = playbackState
)
RxBus.playerStatePublisher.onNext(newState)
Timber.i("New PlaybackState: %s", newState)
}
private fun updateWidget(playerState: PlayerState, song: Track?) {
val started = playerState === PlayerState.STARTED
private fun updateWidget(song: Track?) {
val context = UApp.applicationContext()
UltrasonicAppWidgetProvider4X1.instance?.notifyChange(context, song, started, false)
UltrasonicAppWidgetProvider4X2.instance?.notifyChange(context, song, started, true)
UltrasonicAppWidgetProvider4X3.instance?.notifyChange(context, song, started, false)
UltrasonicAppWidgetProvider4X4.instance?.notifyChange(context, song, started, false)
UltrasonicAppWidgetProvider4X1.instance?.notifyChange(context, song, isPlaying, false)
UltrasonicAppWidgetProvider4X2.instance?.notifyChange(context, song, isPlaying, true)
UltrasonicAppWidgetProvider4X3.instance?.notifyChange(context, song, isPlaying, false)
UltrasonicAppWidgetProvider4X4.instance?.notifyChange(context, song, isPlaying, false)
}
fun onDestroy() {

View File

@ -5,7 +5,6 @@ import io.reactivex.rxjava3.core.Observable
import io.reactivex.rxjava3.disposables.CompositeDisposable
import io.reactivex.rxjava3.disposables.Disposable
import io.reactivex.rxjava3.subjects.PublishSubject
import org.moire.ultrasonic.domain.PlayerState
class RxBus {
companion object {
@ -46,7 +45,12 @@ class RxBus {
dismissNowPlayingCommandPublisher.observeOn(AndroidSchedulers.mainThread())
}
data class StateWithTrack(val state: PlayerState, val track: DownloadFile?, val index: Int = -1)
data class StateWithTrack(
val track: DownloadFile?,
val index: Int = -1,
val isPlaying: Boolean = false,
val state: Int
)
}
operator fun CompositeDisposable.plusAssign(disposable: Disposable) {