From dd6d9da2168102f29d2e0d86a84c6edfd27c265f Mon Sep 17 00:00:00 2001 From: Naveen Date: Tue, 5 Sep 2023 15:08:27 +0530 Subject: [PATCH] Prepare player on resume as well --- .../musicplayer/activities/MainActivity.kt | 16 ++++++---------- .../activities/SimpleControllerActivity.kt | 16 ++++++++++++++++ .../activities/SimpleMusicActivity.kt | 7 ++++--- .../musicplayer/activities/TrackActivity.kt | 2 +- 4 files changed, 27 insertions(+), 14 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/musicplayer/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/musicplayer/activities/MainActivity.kt index 4449cee4..2a1d6ba5 100644 --- a/app/src/main/kotlin/com/simplemobiletools/musicplayer/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/musicplayer/activities/MainActivity.kt @@ -56,6 +56,7 @@ class MainActivity : SimpleMusicActivity() { updateMaterialActivityViews(binding.mainCoordinator, binding.mainHolder, useTransparentNavigation = false, useTopSearchMenu = true) storeStateVariables() setupTabs() + setupCurrentTrackBar(binding.currentTrackBar.root) handlePermission(getPermissionToRequest()) { if (it) { @@ -69,14 +70,6 @@ class MainActivity : SimpleMusicActivity() { volumeControlStream = AudioManager.STREAM_MUSIC checkWhatsNewDialog() checkAppOnSDCard() - withPlayer { - maybePreparePlayer(context = this@MainActivity) { success -> - if (success) { - updateCurrentTrackBar() - broadcastUpdateWidgetState() - } - } - } } override fun onResume() { @@ -188,7 +181,6 @@ class MainActivity : SimpleMusicActivity() { initFragments() binding.sleepTimerStop.setOnClickListener { stopSleepTimer() } - setupCurrentTrackBar(binding.currentTrackBar.root) refreshAllFragments() } @@ -207,7 +199,11 @@ class MainActivity : SimpleMusicActivity() { if (complete) { binding.loadingProgressBar.hide() withPlayer { - sendCommand(CustomCommands.RELOAD_CONTENT) + if (currentMediaItem == null) { + maybePreparePlayer() + } else { + sendCommand(CustomCommands.RELOAD_CONTENT) + } } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/musicplayer/activities/SimpleControllerActivity.kt b/app/src/main/kotlin/com/simplemobiletools/musicplayer/activities/SimpleControllerActivity.kt index 40afc9a5..751defed 100644 --- a/app/src/main/kotlin/com/simplemobiletools/musicplayer/activities/SimpleControllerActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/musicplayer/activities/SimpleControllerActivity.kt @@ -30,6 +30,7 @@ abstract class SimpleControllerActivity : SimpleActivity(), Player.Listener { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) controller = SimpleMediaController.getInstance(this) + maybePreparePlayer() } override fun onStart() { @@ -42,6 +43,13 @@ abstract class SimpleControllerActivity : SimpleActivity(), Player.Listener { controller.removeListener(this) } + override fun onResume() { + super.onResume() + maybePreparePlayer() + } + + open fun onPlayerPrepared(success: Boolean) {} + fun withPlayer(callback: MediaController.() -> Unit) = controller.withController(callback) fun prepareAndPlay(tracks: List, startIndex: Int = 0, startPositionMs: Long = 0, startActivity: Boolean = true) { @@ -60,6 +68,14 @@ abstract class SimpleControllerActivity : SimpleActivity(), Player.Listener { } } + fun maybePreparePlayer() { + withPlayer { + maybePreparePlayer(context = this@SimpleControllerActivity, callback = ::onPlayerPrepared) + } + } + + fun togglePlayback() = withPlayer { togglePlayback() } + fun addTracksToQueue(tracks: List, callback: () -> Unit) { withPlayer { val currentMediaItemsIds = currentMediaItems.map { it.mediaId } diff --git a/app/src/main/kotlin/com/simplemobiletools/musicplayer/activities/SimpleMusicActivity.kt b/app/src/main/kotlin/com/simplemobiletools/musicplayer/activities/SimpleMusicActivity.kt index cca9773a..d100a66a 100644 --- a/app/src/main/kotlin/com/simplemobiletools/musicplayer/activities/SimpleMusicActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/musicplayer/activities/SimpleMusicActivity.kt @@ -8,7 +8,6 @@ import com.simplemobiletools.commons.dialogs.PermissionRequiredDialog import com.simplemobiletools.commons.extensions.hideKeyboard import com.simplemobiletools.commons.extensions.openNotificationSettings import com.simplemobiletools.musicplayer.extensions.isReallyPlaying -import com.simplemobiletools.musicplayer.extensions.togglePlayback import com.simplemobiletools.musicplayer.views.CurrentTrackBar /** @@ -24,7 +23,7 @@ abstract class SimpleMusicActivity : SimpleControllerActivity(), Player.Listener fun setupCurrentTrackBar(trackBar: CurrentTrackBar) { trackBarView = trackBar - trackBarView?.initialize { withPlayer { togglePlayback() } } + trackBarView?.initialize(togglePlayback = ::togglePlayback) trackBarView?.setOnClickListener { hideKeyboard() handleNotificationPermission { granted -> @@ -39,7 +38,7 @@ abstract class SimpleMusicActivity : SimpleControllerActivity(), Player.Listener } } - fun updateCurrentTrackBar() { + private fun updateCurrentTrackBar() { trackBarView?.apply { withPlayer { updateColors() @@ -49,6 +48,8 @@ abstract class SimpleMusicActivity : SimpleControllerActivity(), Player.Listener } } + override fun onPlayerPrepared(success: Boolean) = updateCurrentTrackBar() + @CallSuper override fun onMediaItemTransition(mediaItem: MediaItem?, reason: Int) { trackBarView?.updateCurrentTrack(mediaItem) diff --git a/app/src/main/kotlin/com/simplemobiletools/musicplayer/activities/TrackActivity.kt b/app/src/main/kotlin/com/simplemobiletools/musicplayer/activities/TrackActivity.kt index cc98670d..222672da 100644 --- a/app/src/main/kotlin/com/simplemobiletools/musicplayer/activities/TrackActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/musicplayer/activities/TrackActivity.kt @@ -155,7 +155,7 @@ class TrackActivity : SimpleControllerActivity(), PlaybackSpeedListener { private fun setupButtons() = binding.apply { activityTrackToggleShuffle.setOnClickListener { withPlayer { toggleShuffle() } } activityTrackPrevious.setOnClickListener { withPlayer { forceSeekToPrevious() } } - activityTrackPlayPause.setOnClickListener { withPlayer { togglePlayback() } } + activityTrackPlayPause.setOnClickListener { togglePlayback() } activityTrackNext.setOnClickListener { withPlayer { forceSeekToNext() } } activityTrackProgressCurrent.setOnClickListener { seekBack() } activityTrackProgressMax.setOnClickListener { seekForward() }