Properly handle repeat modes

This commit is contained in:
Naveen 2023-08-21 00:16:04 +05:30
parent e46c39a33f
commit 016070bfaa
No known key found for this signature in database
GPG key ID: 0E155DAD31671DA3
9 changed files with 28 additions and 32 deletions

View file

@ -289,8 +289,7 @@ class TrackActivity : SimpleControllerActivity(), PlaybackSpeedListener {
toast(newPlaybackSetting.descriptionStringRes) toast(newPlaybackSetting.descriptionStringRes)
setupPlaybackSettingButton() setupPlaybackSettingButton()
withPlayer { withPlayer {
setRepeatMode(config.playbackSetting) sendCommand(CustomCommands.TOGGLE_REPEAT_MODE)
setupNextTrackInfo(nextMediaItem)
} }
} }

View file

@ -2,8 +2,6 @@ package com.simplemobiletools.musicplayer.extensions
import androidx.media3.common.MediaItem import androidx.media3.common.MediaItem
import androidx.media3.common.Player import androidx.media3.common.Player
import com.simplemobiletools.musicplayer.helpers.PlaybackSetting
import com.simplemobiletools.musicplayer.playback.player.PlayerListener
val Player.isReallyPlaying: Boolean val Player.isReallyPlaying: Boolean
get() = when (playbackState) { get() = when (playbackState) {
@ -20,15 +18,3 @@ val Player.nextMediaItem: MediaItem?
} else { } else {
null null
} }
/**
* [PlaybackSetting.STOP_AFTER_CURRENT_TRACK] is handled manually as it isn't supported by media3 player. See [PlayerListener.onMediaItemTransition].
*/
fun Player.setRepeatMode(playbackSetting: PlaybackSetting) {
repeatMode = when (playbackSetting) {
PlaybackSetting.REPEAT_OFF -> Player.REPEAT_MODE_OFF
PlaybackSetting.REPEAT_PLAYLIST -> Player.REPEAT_MODE_ALL
PlaybackSetting.REPEAT_TRACK -> Player.REPEAT_MODE_ONE
PlaybackSetting.STOP_AFTER_CURRENT_TRACK -> Player.REPEAT_MODE_ONE
}
}

View file

@ -14,6 +14,7 @@ enum class CustomCommands(val customAction: String) {
CLOSE_PLAYER(customAction = PATH + "CLOSE_PLAYER"), CLOSE_PLAYER(customAction = PATH + "CLOSE_PLAYER"),
RELOAD_CONTENT(customAction = PATH + "RELOAD_CONTENT"), RELOAD_CONTENT(customAction = PATH + "RELOAD_CONTENT"),
SAVE_QUEUE(customAction = PATH + "SAVE_QUEUE"), SAVE_QUEUE(customAction = PATH + "SAVE_QUEUE"),
TOGGLE_REPEAT_MODE(customAction = PATH + "TOGGLE_REPEAT_MODE"),
TOGGLE_SLEEP_TIMER(customAction = PATH + "TOGGLE_SLEEP_TIMER"), TOGGLE_SLEEP_TIMER(customAction = PATH + "TOGGLE_SLEEP_TIMER"),
TOGGLE_SKIP_SILENCE(customAction = PATH + "TOGGLE_SKIP_SILENCE"); TOGGLE_SKIP_SILENCE(customAction = PATH + "TOGGLE_SKIP_SILENCE");

View file

@ -68,6 +68,7 @@ internal fun PlaybackService.getMediaSessionCallback() = object : MediaLibrarySe
CustomCommands.CLOSE_PLAYER -> stopService() CustomCommands.CLOSE_PLAYER -> stopService()
CustomCommands.RELOAD_CONTENT -> reloadContent() CustomCommands.RELOAD_CONTENT -> reloadContent()
CustomCommands.SAVE_QUEUE -> saveRecentItems() CustomCommands.SAVE_QUEUE -> saveRecentItems()
CustomCommands.TOGGLE_REPEAT_MODE -> updateRepeatMode()
CustomCommands.TOGGLE_SLEEP_TIMER -> toggleSleepTimer() CustomCommands.TOGGLE_SLEEP_TIMER -> toggleSleepTimer()
CustomCommands.TOGGLE_SKIP_SILENCE -> player.setSkipSilence(config.gaplessPlayback) CustomCommands.TOGGLE_SKIP_SILENCE -> player.setSkipSilence(config.gaplessPlayback)
} }

View file

@ -11,6 +11,7 @@ import androidx.media3.session.*
import com.simplemobiletools.commons.extensions.hasPermission import com.simplemobiletools.commons.extensions.hasPermission
import com.simplemobiletools.musicplayer.extensions.* import com.simplemobiletools.musicplayer.extensions.*
import com.simplemobiletools.musicplayer.helpers.NotificationHelper import com.simplemobiletools.musicplayer.helpers.NotificationHelper
import com.simplemobiletools.musicplayer.helpers.PlaybackSetting
import com.simplemobiletools.musicplayer.helpers.getPermissionToRequest import com.simplemobiletools.musicplayer.helpers.getPermissionToRequest
import com.simplemobiletools.musicplayer.playback.library.MediaItemProvider import com.simplemobiletools.musicplayer.playback.library.MediaItemProvider
import com.simplemobiletools.musicplayer.playback.player.PlayerListener import com.simplemobiletools.musicplayer.playback.player.PlayerListener
@ -69,7 +70,20 @@ class PlaybackService : MediaLibraryService() {
stopSelf() stopSelf()
} }
internal fun withPlayer(callback: Player.() -> Unit) { internal fun updateRepeatMode() {
withPlayer {
val playbackSetting = config.playbackSetting
setPauseAtEndOfMediaItems(playbackSetting == PlaybackSetting.STOP_AFTER_CURRENT_TRACK)
repeatMode = when (playbackSetting) {
PlaybackSetting.REPEAT_OFF -> Player.REPEAT_MODE_OFF
PlaybackSetting.REPEAT_PLAYLIST -> Player.REPEAT_MODE_ALL
PlaybackSetting.REPEAT_TRACK -> Player.REPEAT_MODE_ONE
PlaybackSetting.STOP_AFTER_CURRENT_TRACK -> Player.REPEAT_MODE_ALL
}
}
}
internal fun withPlayer(callback: SimpleMusicPlayer.() -> Unit) {
player.applicationLooper.post { callback(player) } player.applicationLooper.post { callback(player) }
} }

View file

@ -19,7 +19,7 @@ object SimpleEqualizer {
fun setupEqualizer(context: Context, player: SimpleMusicPlayer) { fun setupEqualizer(context: Context, player: SimpleMusicPlayer) {
try { try {
val preset = context.config.equalizerPreset val preset = context.config.equalizerPreset
instance = Equalizer(0, player.exoPlayer.audioSessionId) instance = Equalizer(0, player.getAudioSessionId())
if (!instance.enabled) { if (!instance.enabled) {
instance.enabled = true instance.enabled = true
} }

View file

@ -14,7 +14,6 @@ import androidx.media3.exoplayer.ExoPlayer
import androidx.media3.session.MediaLibraryService import androidx.media3.session.MediaLibraryService
import com.simplemobiletools.musicplayer.activities.MainActivity import com.simplemobiletools.musicplayer.activities.MainActivity
import com.simplemobiletools.musicplayer.extensions.config import com.simplemobiletools.musicplayer.extensions.config
import com.simplemobiletools.musicplayer.extensions.setRepeatMode
import com.simplemobiletools.musicplayer.helpers.SEEK_INTERVAL_MS import com.simplemobiletools.musicplayer.helpers.SEEK_INTERVAL_MS
import com.simplemobiletools.musicplayer.playback.PlaybackService import com.simplemobiletools.musicplayer.playback.PlaybackService
import com.simplemobiletools.musicplayer.playback.SimpleEqualizer import com.simplemobiletools.musicplayer.playback.SimpleEqualizer
@ -34,7 +33,7 @@ internal fun PlaybackService.initializeSessionAndPlayer(handleAudioFocus: Boolea
withPlayer { withPlayer {
addListener(listener!!) addListener(listener!!)
setRepeatMode(config.playbackSetting) updateRepeatMode()
setPlaybackSpeed(config.playbackSpeed) setPlaybackSpeed(config.playbackSpeed)
shuffleModeEnabled = config.isShuffleEnabled shuffleModeEnabled = config.isShuffleEnabled
mediaSession.setCustomLayout(getCustomLayout()) mediaSession.setCustomLayout(getCustomLayout())

View file

@ -1,16 +1,13 @@
package com.simplemobiletools.musicplayer.playback.player package com.simplemobiletools.musicplayer.playback.player
import android.widget.Toast import android.widget.Toast
import androidx.media3.common.MediaItem
import androidx.media3.common.PlaybackException import androidx.media3.common.PlaybackException
import androidx.media3.common.Player import androidx.media3.common.Player
import androidx.media3.common.util.UnstableApi import androidx.media3.common.util.UnstableApi
import com.simplemobiletools.commons.extensions.toast import com.simplemobiletools.commons.extensions.toast
import com.simplemobiletools.musicplayer.R import com.simplemobiletools.musicplayer.R
import com.simplemobiletools.musicplayer.extensions.broadcastUpdateWidgetState import com.simplemobiletools.musicplayer.extensions.broadcastUpdateWidgetState
import com.simplemobiletools.musicplayer.extensions.config
import com.simplemobiletools.musicplayer.extensions.currentMediaItems import com.simplemobiletools.musicplayer.extensions.currentMediaItems
import com.simplemobiletools.musicplayer.helpers.PlaybackSetting
import com.simplemobiletools.musicplayer.playback.PlaybackService import com.simplemobiletools.musicplayer.playback.PlaybackService
import com.simplemobiletools.musicplayer.playback.PlaybackService.Companion.updatePlaybackInfo import com.simplemobiletools.musicplayer.playback.PlaybackService.Companion.updatePlaybackInfo
@ -36,15 +33,6 @@ class PlayerListener(private val context: PlaybackService) : Player.Listener {
} }
} }
override fun onMediaItemTransition(mediaItem: MediaItem?, reason: Int) {
// We handle this manually because this mode isn't supported in the media3 player.
// It's possible using Exoplayer.setPauseAtEndOfMediaItems() but that would require rebuilding the player.
val isReasonRepeat = reason == Player.MEDIA_ITEM_TRANSITION_REASON_REPEAT
if (isReasonRepeat && context.config.playbackSetting == PlaybackSetting.STOP_AFTER_CURRENT_TRACK) {
player.pause()
}
}
private fun updatePlaybackInfo() { private fun updatePlaybackInfo() {
updatePlaybackInfo(player) updatePlaybackInfo(player)
val currentMediaItem = player.currentMediaItem val currentMediaItem = player.currentMediaItem

View file

@ -29,7 +29,15 @@ class SimpleMusicPlayer(val exoPlayer: ExoPlayer) : ForwardingPlayer(exoPlayer)
playWhenReady = true playWhenReady = true
} }
fun getAudioSessionId(): Int {
return exoPlayer.audioSessionId
}
fun setSkipSilence(skipSilence: Boolean) { fun setSkipSilence(skipSilence: Boolean) {
exoPlayer.skipSilenceEnabled = skipSilence exoPlayer.skipSilenceEnabled = skipSilence
} }
fun setPauseAtEndOfMediaItems(pauseAtEnd: Boolean) {
exoPlayer.pauseAtEndOfMediaItems = pauseAtEnd
}
} }