Prepare player on resume as well

This commit is contained in:
Naveen 2023-09-05 15:08:27 +05:30
parent a2103dcdd2
commit dd6d9da216
No known key found for this signature in database
GPG key ID: 0E155DAD31671DA3
4 changed files with 27 additions and 14 deletions

View file

@ -56,6 +56,7 @@ class MainActivity : SimpleMusicActivity() {
updateMaterialActivityViews(binding.mainCoordinator, binding.mainHolder, useTransparentNavigation = false, useTopSearchMenu = true) updateMaterialActivityViews(binding.mainCoordinator, binding.mainHolder, useTransparentNavigation = false, useTopSearchMenu = true)
storeStateVariables() storeStateVariables()
setupTabs() setupTabs()
setupCurrentTrackBar(binding.currentTrackBar.root)
handlePermission(getPermissionToRequest()) { handlePermission(getPermissionToRequest()) {
if (it) { if (it) {
@ -69,14 +70,6 @@ class MainActivity : SimpleMusicActivity() {
volumeControlStream = AudioManager.STREAM_MUSIC volumeControlStream = AudioManager.STREAM_MUSIC
checkWhatsNewDialog() checkWhatsNewDialog()
checkAppOnSDCard() checkAppOnSDCard()
withPlayer {
maybePreparePlayer(context = this@MainActivity) { success ->
if (success) {
updateCurrentTrackBar()
broadcastUpdateWidgetState()
}
}
}
} }
override fun onResume() { override fun onResume() {
@ -188,7 +181,6 @@ class MainActivity : SimpleMusicActivity() {
initFragments() initFragments()
binding.sleepTimerStop.setOnClickListener { stopSleepTimer() } binding.sleepTimerStop.setOnClickListener { stopSleepTimer() }
setupCurrentTrackBar(binding.currentTrackBar.root)
refreshAllFragments() refreshAllFragments()
} }
@ -207,6 +199,9 @@ class MainActivity : SimpleMusicActivity() {
if (complete) { if (complete) {
binding.loadingProgressBar.hide() binding.loadingProgressBar.hide()
withPlayer { withPlayer {
if (currentMediaItem == null) {
maybePreparePlayer()
} else {
sendCommand(CustomCommands.RELOAD_CONTENT) sendCommand(CustomCommands.RELOAD_CONTENT)
} }
} }
@ -214,6 +209,7 @@ class MainActivity : SimpleMusicActivity() {
} }
} }
} }
}
private fun initFragments() { private fun initFragments() {
binding.viewPager.adapter = ViewPagerAdapter(this) binding.viewPager.adapter = ViewPagerAdapter(this)

View file

@ -30,6 +30,7 @@ abstract class SimpleControllerActivity : SimpleActivity(), Player.Listener {
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
controller = SimpleMediaController.getInstance(this) controller = SimpleMediaController.getInstance(this)
maybePreparePlayer()
} }
override fun onStart() { override fun onStart() {
@ -42,6 +43,13 @@ abstract class SimpleControllerActivity : SimpleActivity(), Player.Listener {
controller.removeListener(this) controller.removeListener(this)
} }
override fun onResume() {
super.onResume()
maybePreparePlayer()
}
open fun onPlayerPrepared(success: Boolean) {}
fun withPlayer(callback: MediaController.() -> Unit) = controller.withController(callback) fun withPlayer(callback: MediaController.() -> Unit) = controller.withController(callback)
fun prepareAndPlay(tracks: List<Track>, startIndex: Int = 0, startPositionMs: Long = 0, startActivity: Boolean = true) { fun prepareAndPlay(tracks: List<Track>, 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<Track>, callback: () -> Unit) { fun addTracksToQueue(tracks: List<Track>, callback: () -> Unit) {
withPlayer { withPlayer {
val currentMediaItemsIds = currentMediaItems.map { it.mediaId } val currentMediaItemsIds = currentMediaItems.map { it.mediaId }

View file

@ -8,7 +8,6 @@ import com.simplemobiletools.commons.dialogs.PermissionRequiredDialog
import com.simplemobiletools.commons.extensions.hideKeyboard import com.simplemobiletools.commons.extensions.hideKeyboard
import com.simplemobiletools.commons.extensions.openNotificationSettings import com.simplemobiletools.commons.extensions.openNotificationSettings
import com.simplemobiletools.musicplayer.extensions.isReallyPlaying import com.simplemobiletools.musicplayer.extensions.isReallyPlaying
import com.simplemobiletools.musicplayer.extensions.togglePlayback
import com.simplemobiletools.musicplayer.views.CurrentTrackBar import com.simplemobiletools.musicplayer.views.CurrentTrackBar
/** /**
@ -24,7 +23,7 @@ abstract class SimpleMusicActivity : SimpleControllerActivity(), Player.Listener
fun setupCurrentTrackBar(trackBar: CurrentTrackBar) { fun setupCurrentTrackBar(trackBar: CurrentTrackBar) {
trackBarView = trackBar trackBarView = trackBar
trackBarView?.initialize { withPlayer { togglePlayback() } } trackBarView?.initialize(togglePlayback = ::togglePlayback)
trackBarView?.setOnClickListener { trackBarView?.setOnClickListener {
hideKeyboard() hideKeyboard()
handleNotificationPermission { granted -> handleNotificationPermission { granted ->
@ -39,7 +38,7 @@ abstract class SimpleMusicActivity : SimpleControllerActivity(), Player.Listener
} }
} }
fun updateCurrentTrackBar() { private fun updateCurrentTrackBar() {
trackBarView?.apply { trackBarView?.apply {
withPlayer { withPlayer {
updateColors() updateColors()
@ -49,6 +48,8 @@ abstract class SimpleMusicActivity : SimpleControllerActivity(), Player.Listener
} }
} }
override fun onPlayerPrepared(success: Boolean) = updateCurrentTrackBar()
@CallSuper @CallSuper
override fun onMediaItemTransition(mediaItem: MediaItem?, reason: Int) { override fun onMediaItemTransition(mediaItem: MediaItem?, reason: Int) {
trackBarView?.updateCurrentTrack(mediaItem) trackBarView?.updateCurrentTrack(mediaItem)

View file

@ -155,7 +155,7 @@ class TrackActivity : SimpleControllerActivity(), PlaybackSpeedListener {
private fun setupButtons() = binding.apply { private fun setupButtons() = binding.apply {
activityTrackToggleShuffle.setOnClickListener { withPlayer { toggleShuffle() } } activityTrackToggleShuffle.setOnClickListener { withPlayer { toggleShuffle() } }
activityTrackPrevious.setOnClickListener { withPlayer { forceSeekToPrevious() } } activityTrackPrevious.setOnClickListener { withPlayer { forceSeekToPrevious() } }
activityTrackPlayPause.setOnClickListener { withPlayer { togglePlayback() } } activityTrackPlayPause.setOnClickListener { togglePlayback() }
activityTrackNext.setOnClickListener { withPlayer { forceSeekToNext() } } activityTrackNext.setOnClickListener { withPlayer { forceSeekToNext() } }
activityTrackProgressCurrent.setOnClickListener { seekBack() } activityTrackProgressCurrent.setOnClickListener { seekBack() }
activityTrackProgressMax.setOnClickListener { seekForward() } activityTrackProgressMax.setOnClickListener { seekForward() }