mirror of
https://github.com/ultrasonic/ultrasonic
synced 2025-02-16 19:50:35 +01:00
Remove legacyPlayerState
This commit is contained in:
parent
647435fe55
commit
6115ac995f
@ -18,6 +18,8 @@
|
|||||||
*/
|
*/
|
||||||
package org.moire.ultrasonic.view;
|
package org.moire.ultrasonic.view;
|
||||||
|
|
||||||
|
import static org.koin.java.KoinJavaComponent.inject;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.graphics.Canvas;
|
import android.graphics.Canvas;
|
||||||
import android.graphics.Color;
|
import android.graphics.Color;
|
||||||
@ -29,14 +31,11 @@ import androidx.lifecycle.LifecycleOwner;
|
|||||||
import androidx.lifecycle.Observer;
|
import androidx.lifecycle.Observer;
|
||||||
|
|
||||||
import org.moire.ultrasonic.audiofx.VisualizerController;
|
import org.moire.ultrasonic.audiofx.VisualizerController;
|
||||||
import org.moire.ultrasonic.domain.PlayerState;
|
|
||||||
import org.moire.ultrasonic.service.MediaPlayerController;
|
import org.moire.ultrasonic.service.MediaPlayerController;
|
||||||
|
|
||||||
import kotlin.Lazy;
|
import kotlin.Lazy;
|
||||||
import timber.log.Timber;
|
import timber.log.Timber;
|
||||||
|
|
||||||
import static org.koin.java.KoinJavaComponent.inject;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A simple class that draws waveform data received from a
|
* A simple class that draws waveform data received from a
|
||||||
* {@link Visualizer.OnDataCaptureListener#onWaveFormDataCapture}
|
* {@link Visualizer.OnDataCaptureListener#onWaveFormDataCapture}
|
||||||
@ -130,7 +129,7 @@ public class VisualizerView extends View
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mediaPlayerControllerLazy.getValue().getLegacyPlayerState() != PlayerState.STARTED)
|
if (!mediaPlayerControllerLazy.getValue().isPlaying())
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -46,7 +46,6 @@ import org.koin.androidx.viewmodel.ext.android.viewModel
|
|||||||
import org.moire.ultrasonic.R
|
import org.moire.ultrasonic.R
|
||||||
import org.moire.ultrasonic.data.ActiveServerProvider
|
import org.moire.ultrasonic.data.ActiveServerProvider
|
||||||
import org.moire.ultrasonic.data.ServerSettingDao
|
import org.moire.ultrasonic.data.ServerSettingDao
|
||||||
import org.moire.ultrasonic.domain.PlayerState
|
|
||||||
import org.moire.ultrasonic.fragment.OnBackPressedHandler
|
import org.moire.ultrasonic.fragment.OnBackPressedHandler
|
||||||
import org.moire.ultrasonic.model.ServerSettingsModel
|
import org.moire.ultrasonic.model.ServerSettingsModel
|
||||||
import org.moire.ultrasonic.provider.SearchSuggestionProvider
|
import org.moire.ultrasonic.provider.SearchSuggestionProvider
|
||||||
@ -183,7 +182,7 @@ class NavigationActivity : AppCompatActivity() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
rxBusSubscription += RxBus.playerStateObservable.subscribe {
|
rxBusSubscription += RxBus.playerStateObservable.subscribe {
|
||||||
if (it.state === PlayerState.STARTED || it.state === PlayerState.PAUSED)
|
if (it.state == STATE_READY)
|
||||||
showNowPlaying()
|
showNowPlaying()
|
||||||
else
|
else
|
||||||
hideNowPlaying()
|
hideNowPlaying()
|
||||||
|
@ -109,6 +109,7 @@ class TrackViewHolder(val view: View) : RecyclerView.ViewHolder(view), Checkable
|
|||||||
}
|
}
|
||||||
|
|
||||||
rxSubscription = RxBus.playerStateObservable.subscribe {
|
rxSubscription = RxBus.playerStateObservable.subscribe {
|
||||||
|
Timber.i("NEW PLAY STATE")
|
||||||
setPlayIcon(it.index == bindingAdapterPosition && it.track == downloadFile)
|
setPlayIcon(it.index == bindingAdapterPosition && it.track == downloadFile)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -22,7 +22,6 @@ import java.lang.Exception
|
|||||||
import kotlin.math.abs
|
import kotlin.math.abs
|
||||||
import org.koin.android.ext.android.inject
|
import org.koin.android.ext.android.inject
|
||||||
import org.moire.ultrasonic.R
|
import org.moire.ultrasonic.R
|
||||||
import org.moire.ultrasonic.domain.PlayerState
|
|
||||||
import org.moire.ultrasonic.service.MediaPlayerController
|
import org.moire.ultrasonic.service.MediaPlayerController
|
||||||
import org.moire.ultrasonic.service.RxBus
|
import org.moire.ultrasonic.service.RxBus
|
||||||
import org.moire.ultrasonic.subsonic.ImageLoaderProvider
|
import org.moire.ultrasonic.subsonic.ImageLoaderProvider
|
||||||
@ -85,20 +84,18 @@ class NowPlayingFragment : Fragment() {
|
|||||||
@SuppressLint("ClickableViewAccessibility")
|
@SuppressLint("ClickableViewAccessibility")
|
||||||
private fun update() {
|
private fun update() {
|
||||||
try {
|
try {
|
||||||
val playerState = mediaPlayerController.legacyPlayerState
|
if (mediaPlayerController.isPlaying) {
|
||||||
|
|
||||||
if (playerState === PlayerState.PAUSED) {
|
|
||||||
playButton!!.setImageDrawable(
|
|
||||||
getDrawableFromAttribute(
|
|
||||||
requireContext(), R.attr.media_play
|
|
||||||
)
|
|
||||||
)
|
|
||||||
} else if (playerState === PlayerState.STARTED) {
|
|
||||||
playButton!!.setImageDrawable(
|
playButton!!.setImageDrawable(
|
||||||
getDrawableFromAttribute(
|
getDrawableFromAttribute(
|
||||||
requireContext(), R.attr.media_pause
|
requireContext(), R.attr.media_pause
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
} else {
|
||||||
|
playButton!!.setImageDrawable(
|
||||||
|
getDrawableFromAttribute(
|
||||||
|
requireContext(), R.attr.media_play
|
||||||
|
)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
val file = mediaPlayerController.currentPlayingLegacy
|
val file = mediaPlayerController.currentPlayingLegacy
|
||||||
|
@ -13,7 +13,6 @@ import androidx.core.net.toUri
|
|||||||
import androidx.media3.common.MediaItem
|
import androidx.media3.common.MediaItem
|
||||||
import androidx.media3.common.MediaMetadata
|
import androidx.media3.common.MediaMetadata
|
||||||
import androidx.media3.common.Player
|
import androidx.media3.common.Player
|
||||||
import androidx.media3.common.Player.STATE_BUFFERING
|
|
||||||
import androidx.media3.common.Timeline
|
import androidx.media3.common.Timeline
|
||||||
import androidx.media3.session.MediaController
|
import androidx.media3.session.MediaController
|
||||||
import androidx.media3.session.SessionToken
|
import androidx.media3.session.SessionToken
|
||||||
@ -91,13 +90,11 @@ class MediaPlayerController(
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun onPlaybackStateChanged(playbackState: Int) {
|
override fun onPlaybackStateChanged(playbackState: Int) {
|
||||||
translatePlaybackState(playbackState = playbackState)
|
|
||||||
playerStateChangedHandler()
|
playerStateChangedHandler()
|
||||||
publishPlaybackState()
|
publishPlaybackState()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onIsPlayingChanged(isPlaying: Boolean) {
|
override fun onIsPlayingChanged(isPlaying: Boolean) {
|
||||||
translatePlaybackState(isPlaying = isPlaying)
|
|
||||||
playerStateChangedHandler()
|
playerStateChangedHandler()
|
||||||
publishPlaybackState()
|
publishPlaybackState()
|
||||||
}
|
}
|
||||||
@ -121,57 +118,29 @@ class MediaPlayerController(
|
|||||||
Timber.i("MediaPlayerController created")
|
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() {
|
private fun playerStateChangedHandler() {
|
||||||
|
|
||||||
val playerState = legacyPlayerState
|
|
||||||
val currentPlaying = legacyPlaylistManager.currentPlaying
|
val currentPlaying = legacyPlaylistManager.currentPlaying
|
||||||
|
|
||||||
when {
|
when (playbackState) {
|
||||||
playerState === PlayerState.PAUSED -> {
|
Player.STATE_READY -> {
|
||||||
playbackStateSerializer.serialize(
|
if (isPlaying) {
|
||||||
playList, currentMediaItemIndex, playerPosition
|
scrobbler.scrobble(currentPlaying, false)
|
||||||
)
|
} else {
|
||||||
|
playbackStateSerializer.serialize(
|
||||||
|
playList, currentMediaItemIndex, playerPosition
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
playerState === PlayerState.STARTED -> {
|
Player.STATE_ENDED -> {
|
||||||
scrobbler.scrobble(currentPlaying, false)
|
|
||||||
}
|
|
||||||
playerState === PlayerState.COMPLETED -> {
|
|
||||||
scrobbler.scrobble(currentPlaying, true)
|
scrobbler.scrobble(currentPlaying, true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update widget
|
// Update widget
|
||||||
if (currentPlaying != null) {
|
if (currentPlaying != null) {
|
||||||
updateWidget(playerState, currentPlaying.track)
|
updateWidget(currentPlaying.track)
|
||||||
}
|
}
|
||||||
|
|
||||||
Timber.d("Processed player state change")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun onTrackCompleted() {
|
private fun onTrackCompleted() {
|
||||||
@ -190,23 +159,23 @@ class MediaPlayerController(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun publishPlaybackState() {
|
private fun publishPlaybackState() {
|
||||||
RxBus.playerStatePublisher.onNext(
|
val newState = RxBus.StateWithTrack(
|
||||||
RxBus.StateWithTrack(
|
track = legacyPlaylistManager.currentPlaying,
|
||||||
state = legacyPlayerState,
|
index = currentMediaItemIndex,
|
||||||
track = legacyPlaylistManager.currentPlaying,
|
isPlaying = isPlaying,
|
||||||
index = currentMediaItemIndex
|
state = playbackState
|
||||||
)
|
|
||||||
)
|
)
|
||||||
|
RxBus.playerStatePublisher.onNext(newState)
|
||||||
|
Timber.i("New PlaybackState: %s", newState)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun updateWidget(playerState: PlayerState, song: Track?) {
|
private fun updateWidget(song: Track?) {
|
||||||
val started = playerState === PlayerState.STARTED
|
|
||||||
val context = UApp.applicationContext()
|
val context = UApp.applicationContext()
|
||||||
|
|
||||||
UltrasonicAppWidgetProvider4X1.instance?.notifyChange(context, song, started, false)
|
UltrasonicAppWidgetProvider4X1.instance?.notifyChange(context, song, isPlaying, false)
|
||||||
UltrasonicAppWidgetProvider4X2.instance?.notifyChange(context, song, started, true)
|
UltrasonicAppWidgetProvider4X2.instance?.notifyChange(context, song, isPlaying, true)
|
||||||
UltrasonicAppWidgetProvider4X3.instance?.notifyChange(context, song, started, false)
|
UltrasonicAppWidgetProvider4X3.instance?.notifyChange(context, song, isPlaying, false)
|
||||||
UltrasonicAppWidgetProvider4X4.instance?.notifyChange(context, song, started, false)
|
UltrasonicAppWidgetProvider4X4.instance?.notifyChange(context, song, isPlaying, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun onDestroy() {
|
fun onDestroy() {
|
||||||
|
@ -5,7 +5,6 @@ import io.reactivex.rxjava3.core.Observable
|
|||||||
import io.reactivex.rxjava3.disposables.CompositeDisposable
|
import io.reactivex.rxjava3.disposables.CompositeDisposable
|
||||||
import io.reactivex.rxjava3.disposables.Disposable
|
import io.reactivex.rxjava3.disposables.Disposable
|
||||||
import io.reactivex.rxjava3.subjects.PublishSubject
|
import io.reactivex.rxjava3.subjects.PublishSubject
|
||||||
import org.moire.ultrasonic.domain.PlayerState
|
|
||||||
|
|
||||||
class RxBus {
|
class RxBus {
|
||||||
companion object {
|
companion object {
|
||||||
@ -46,7 +45,12 @@ class RxBus {
|
|||||||
dismissNowPlayingCommandPublisher.observeOn(AndroidSchedulers.mainThread())
|
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) {
|
operator fun CompositeDisposable.plusAssign(disposable: Disposable) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user