Make PlaybackStateManager use millis

Make PlaybackStateManager use millis for the current position instead of seconds, in order to be more in line with the rest of the code.
This commit is contained in:
OxygenCobalt 2020-10-31 14:32:44 -06:00
parent e00930cc5f
commit 909cea01fa
3 changed files with 22 additions and 18 deletions

View file

@ -47,7 +47,7 @@ class PlaybackService : Service(), Player.EventListener, PlaybackStateCallback {
private val playbackManager = PlaybackStateManager.getInstance() private val playbackManager = PlaybackStateManager.getInstance()
private lateinit var mediaSession: MediaSessionCompat private lateinit var mediaSession: MediaSessionCompat
private lateinit var systemReceiver: SystemEventReceiver private lateinit var systemReceiver: SystemEventReceiver
private val notificationHolder = PlaybackNotificationHolder() private lateinit var notificationHolder: PlaybackNotificationHolder
private var changeIsFromAudioFocus = true private var changeIsFromAudioFocus = true
@ -70,6 +70,7 @@ class PlaybackService : Service(), Player.EventListener, PlaybackStateCallback {
super.onCreate() super.onCreate()
// --- PLAYER SETUP --- // --- PLAYER SETUP ---
player.addListener(this) player.addListener(this)
// Set up AudioFocus/AudioAttributes // Set up AudioFocus/AudioAttributes
@ -85,14 +86,6 @@ class PlaybackService : Service(), Player.EventListener, PlaybackStateCallback {
player.experimentalSetOffloadSchedulingEnabled(true) player.experimentalSetOffloadSchedulingEnabled(true)
} }
// --- PLAYBACKSTATEMANAGER SETUP ---
playbackManager.addCallback(this)
if (playbackManager.song != null) {
restorePlayer()
}
// --- SYSTEM RECEIVER SETUP --- // --- SYSTEM RECEIVER SETUP ---
// Set up the media button callbacks // Set up the media button callbacks
@ -120,7 +113,17 @@ class PlaybackService : Service(), Player.EventListener, PlaybackStateCallback {
// --- NOTIFICATION SETUP --- // --- NOTIFICATION SETUP ---
notificationHolder = PlaybackNotificationHolder()
notificationHolder.init(applicationContext, mediaSession) notificationHolder.init(applicationContext, mediaSession)
// --- PLAYBACKSTATEMANAGER SETUP ---
playbackManager.addCallback(this)
if (playbackManager.song != null) {
restorePlayer()
}
} }
override fun onDestroy() { override fun onDestroy() {
@ -156,6 +159,8 @@ class PlaybackService : Service(), Player.EventListener, PlaybackStateCallback {
if (isPlaying != playbackManager.isPlaying && changeIsFromAudioFocus) { if (isPlaying != playbackManager.isPlaying && changeIsFromAudioFocus) {
playbackManager.setPlayingStatus(isPlaying) playbackManager.setPlayingStatus(isPlaying)
} }
changeIsFromAudioFocus = true
} }
override fun onMediaItemTransition(mediaItem: MediaItem?, reason: Int) { override fun onMediaItemTransition(mediaItem: MediaItem?, reason: Int) {
@ -174,14 +179,13 @@ class PlaybackService : Service(), Player.EventListener, PlaybackStateCallback {
override fun onPositionDiscontinuity(reason: Int) { override fun onPositionDiscontinuity(reason: Int) {
if (reason == Player.DISCONTINUITY_REASON_SEEK) { if (reason == Player.DISCONTINUITY_REASON_SEEK) {
playbackManager.setPosition(player.currentPosition / 1000) playbackManager.setPosition(player.currentPosition)
} }
} }
// --- PLAYBACK STATE CALLBACK OVERRIDES --- // --- PLAYBACK STATE CALLBACK OVERRIDES ---
override fun onSongUpdate(song: Song?) { override fun onSongUpdate(song: Song?) {
changeIsFromAudioFocus = false
song?.let { song?.let {
val item = MediaItem.fromUri(it.id.toURI()) val item = MediaItem.fromUri(it.id.toURI())
@ -232,7 +236,7 @@ class PlaybackService : Service(), Player.EventListener, PlaybackStateCallback {
override fun onSeekConfirm(position: Long) { override fun onSeekConfirm(position: Long) {
changeIsFromAudioFocus = false changeIsFromAudioFocus = false
player.seekTo(position * 1000) player.seekTo(position)
} }
// --- OTHER FUNCTIONS --- // --- OTHER FUNCTIONS ---
@ -242,7 +246,7 @@ class PlaybackService : Service(), Player.EventListener, PlaybackStateCallback {
val item = MediaItem.fromUri(it.id.toURI()) val item = MediaItem.fromUri(it.id.toURI())
player.setMediaItem(item) player.setMediaItem(item)
player.prepare() player.prepare()
player.play() player.seekTo(playbackManager.position)
notificationHolder.setMetadata(it, this) notificationHolder.setMetadata(it, this)
} }
@ -278,7 +282,7 @@ class PlaybackService : Service(), Player.EventListener, PlaybackStateCallback {
private fun startPollingPosition() { private fun startPollingPosition() {
serviceScope.launch { serviceScope.launch {
pollCurrentPosition().takeWhile { player.isPlaying }.collect { pollCurrentPosition().takeWhile { player.isPlaying }.collect {
playbackManager.setPosition(it / 1000) playbackManager.setPosition(it)
} }
} }
} }

View file

@ -129,7 +129,7 @@ class PlaybackViewModel : ViewModel(), PlaybackStateCallback {
// Update the position and push the change the playbackManager. // Update the position and push the change the playbackManager.
fun updatePosition(progress: Int) { fun updatePosition(progress: Int) {
playbackManager.seekTo(progress.toLong()) playbackManager.seekTo((progress * 1000).toLong())
} }
// --- QUEUE FUNCTIONS --- // --- QUEUE FUNCTIONS ---
@ -199,7 +199,7 @@ class PlaybackViewModel : ViewModel(), PlaybackStateCallback {
override fun onPositionUpdate(position: Long) { override fun onPositionUpdate(position: Long) {
if (!mIsSeeking.value!!) { if (!mIsSeeking.value!!) {
mPosition.value = position mPosition.value = position / 1000
} }
} }
@ -227,7 +227,7 @@ class PlaybackViewModel : ViewModel(), PlaybackStateCallback {
Log.d(this::class.simpleName, "Attempting to restore playback state.") Log.d(this::class.simpleName, "Attempting to restore playback state.")
mSong.value = playbackManager.song mSong.value = playbackManager.song
mPosition.value = playbackManager.position mPosition.value = playbackManager.position / 1000
mQueue.value = playbackManager.queue mQueue.value = playbackManager.queue
mIndex.value = playbackManager.index mIndex.value = playbackManager.index
mIsPlaying.value = playbackManager.isPlaying mIsPlaying.value = playbackManager.isPlaying

View file

@ -181,7 +181,7 @@ internal class PlaybackStateManager {
// Due to the hacky way I poll ExoPlayer positions, don't accept any bugged positions // Due to the hacky way I poll ExoPlayer positions, don't accept any bugged positions
// that are over the duration of the song. // that are over the duration of the song.
mSong?.let { mSong?.let {
if (position <= it.seconds) { if (position <= it.duration) {
mPosition = position mPosition = position
} }
} }