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 4c09836a..109eba21 100644 --- a/app/src/main/kotlin/com/simplemobiletools/musicplayer/activities/TrackActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/musicplayer/activities/TrackActivity.kt @@ -289,8 +289,7 @@ class TrackActivity : SimpleControllerActivity(), PlaybackSpeedListener { toast(newPlaybackSetting.descriptionStringRes) setupPlaybackSettingButton() withPlayer { - setRepeatMode(config.playbackSetting) - setupNextTrackInfo(nextMediaItem) + sendCommand(CustomCommands.TOGGLE_REPEAT_MODE) } } diff --git a/app/src/main/kotlin/com/simplemobiletools/musicplayer/extensions/Player.kt b/app/src/main/kotlin/com/simplemobiletools/musicplayer/extensions/Player.kt index a443090d..744db1dd 100644 --- a/app/src/main/kotlin/com/simplemobiletools/musicplayer/extensions/Player.kt +++ b/app/src/main/kotlin/com/simplemobiletools/musicplayer/extensions/Player.kt @@ -2,8 +2,6 @@ package com.simplemobiletools.musicplayer.extensions import androidx.media3.common.MediaItem import androidx.media3.common.Player -import com.simplemobiletools.musicplayer.helpers.PlaybackSetting -import com.simplemobiletools.musicplayer.playback.player.PlayerListener val Player.isReallyPlaying: Boolean get() = when (playbackState) { @@ -20,15 +18,3 @@ val Player.nextMediaItem: MediaItem? } else { 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 - } -} diff --git a/app/src/main/kotlin/com/simplemobiletools/musicplayer/playback/CustomCommands.kt b/app/src/main/kotlin/com/simplemobiletools/musicplayer/playback/CustomCommands.kt index 240872bd..2b2c108b 100644 --- a/app/src/main/kotlin/com/simplemobiletools/musicplayer/playback/CustomCommands.kt +++ b/app/src/main/kotlin/com/simplemobiletools/musicplayer/playback/CustomCommands.kt @@ -14,6 +14,7 @@ enum class CustomCommands(val customAction: String) { CLOSE_PLAYER(customAction = PATH + "CLOSE_PLAYER"), RELOAD_CONTENT(customAction = PATH + "RELOAD_CONTENT"), SAVE_QUEUE(customAction = PATH + "SAVE_QUEUE"), + TOGGLE_REPEAT_MODE(customAction = PATH + "TOGGLE_REPEAT_MODE"), TOGGLE_SLEEP_TIMER(customAction = PATH + "TOGGLE_SLEEP_TIMER"), TOGGLE_SKIP_SILENCE(customAction = PATH + "TOGGLE_SKIP_SILENCE"); diff --git a/app/src/main/kotlin/com/simplemobiletools/musicplayer/playback/MediaSessionCallback.kt b/app/src/main/kotlin/com/simplemobiletools/musicplayer/playback/MediaSessionCallback.kt index 9e28e38b..d1325323 100644 --- a/app/src/main/kotlin/com/simplemobiletools/musicplayer/playback/MediaSessionCallback.kt +++ b/app/src/main/kotlin/com/simplemobiletools/musicplayer/playback/MediaSessionCallback.kt @@ -68,6 +68,7 @@ internal fun PlaybackService.getMediaSessionCallback() = object : MediaLibrarySe CustomCommands.CLOSE_PLAYER -> stopService() CustomCommands.RELOAD_CONTENT -> reloadContent() CustomCommands.SAVE_QUEUE -> saveRecentItems() + CustomCommands.TOGGLE_REPEAT_MODE -> updateRepeatMode() CustomCommands.TOGGLE_SLEEP_TIMER -> toggleSleepTimer() CustomCommands.TOGGLE_SKIP_SILENCE -> player.setSkipSilence(config.gaplessPlayback) } diff --git a/app/src/main/kotlin/com/simplemobiletools/musicplayer/playback/PlaybackService.kt b/app/src/main/kotlin/com/simplemobiletools/musicplayer/playback/PlaybackService.kt index b4251245..dc1aca12 100644 --- a/app/src/main/kotlin/com/simplemobiletools/musicplayer/playback/PlaybackService.kt +++ b/app/src/main/kotlin/com/simplemobiletools/musicplayer/playback/PlaybackService.kt @@ -11,6 +11,7 @@ import androidx.media3.session.* import com.simplemobiletools.commons.extensions.hasPermission import com.simplemobiletools.musicplayer.extensions.* import com.simplemobiletools.musicplayer.helpers.NotificationHelper +import com.simplemobiletools.musicplayer.helpers.PlaybackSetting import com.simplemobiletools.musicplayer.helpers.getPermissionToRequest import com.simplemobiletools.musicplayer.playback.library.MediaItemProvider import com.simplemobiletools.musicplayer.playback.player.PlayerListener @@ -69,7 +70,20 @@ class PlaybackService : MediaLibraryService() { 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) } } diff --git a/app/src/main/kotlin/com/simplemobiletools/musicplayer/playback/SimpleEqualizer.kt b/app/src/main/kotlin/com/simplemobiletools/musicplayer/playback/SimpleEqualizer.kt index 65036219..9f0d6bb2 100644 --- a/app/src/main/kotlin/com/simplemobiletools/musicplayer/playback/SimpleEqualizer.kt +++ b/app/src/main/kotlin/com/simplemobiletools/musicplayer/playback/SimpleEqualizer.kt @@ -19,7 +19,7 @@ object SimpleEqualizer { fun setupEqualizer(context: Context, player: SimpleMusicPlayer) { try { val preset = context.config.equalizerPreset - instance = Equalizer(0, player.exoPlayer.audioSessionId) + instance = Equalizer(0, player.getAudioSessionId()) if (!instance.enabled) { instance.enabled = true } diff --git a/app/src/main/kotlin/com/simplemobiletools/musicplayer/playback/player/Player.kt b/app/src/main/kotlin/com/simplemobiletools/musicplayer/playback/player/Player.kt index 5cbbef2a..1637336c 100644 --- a/app/src/main/kotlin/com/simplemobiletools/musicplayer/playback/player/Player.kt +++ b/app/src/main/kotlin/com/simplemobiletools/musicplayer/playback/player/Player.kt @@ -14,7 +14,6 @@ import androidx.media3.exoplayer.ExoPlayer import androidx.media3.session.MediaLibraryService import com.simplemobiletools.musicplayer.activities.MainActivity import com.simplemobiletools.musicplayer.extensions.config -import com.simplemobiletools.musicplayer.extensions.setRepeatMode import com.simplemobiletools.musicplayer.helpers.SEEK_INTERVAL_MS import com.simplemobiletools.musicplayer.playback.PlaybackService import com.simplemobiletools.musicplayer.playback.SimpleEqualizer @@ -34,7 +33,7 @@ internal fun PlaybackService.initializeSessionAndPlayer(handleAudioFocus: Boolea withPlayer { addListener(listener!!) - setRepeatMode(config.playbackSetting) + updateRepeatMode() setPlaybackSpeed(config.playbackSpeed) shuffleModeEnabled = config.isShuffleEnabled mediaSession.setCustomLayout(getCustomLayout()) diff --git a/app/src/main/kotlin/com/simplemobiletools/musicplayer/playback/player/PlayerListener.kt b/app/src/main/kotlin/com/simplemobiletools/musicplayer/playback/player/PlayerListener.kt index ab3cb391..84ad0557 100644 --- a/app/src/main/kotlin/com/simplemobiletools/musicplayer/playback/player/PlayerListener.kt +++ b/app/src/main/kotlin/com/simplemobiletools/musicplayer/playback/player/PlayerListener.kt @@ -1,16 +1,13 @@ package com.simplemobiletools.musicplayer.playback.player import android.widget.Toast -import androidx.media3.common.MediaItem import androidx.media3.common.PlaybackException import androidx.media3.common.Player import androidx.media3.common.util.UnstableApi import com.simplemobiletools.commons.extensions.toast import com.simplemobiletools.musicplayer.R import com.simplemobiletools.musicplayer.extensions.broadcastUpdateWidgetState -import com.simplemobiletools.musicplayer.extensions.config import com.simplemobiletools.musicplayer.extensions.currentMediaItems -import com.simplemobiletools.musicplayer.helpers.PlaybackSetting import com.simplemobiletools.musicplayer.playback.PlaybackService 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() { updatePlaybackInfo(player) val currentMediaItem = player.currentMediaItem diff --git a/app/src/main/kotlin/com/simplemobiletools/musicplayer/playback/player/SimpleMusicPlayer.kt b/app/src/main/kotlin/com/simplemobiletools/musicplayer/playback/player/SimpleMusicPlayer.kt index 6540bcb7..12fa6b6c 100644 --- a/app/src/main/kotlin/com/simplemobiletools/musicplayer/playback/player/SimpleMusicPlayer.kt +++ b/app/src/main/kotlin/com/simplemobiletools/musicplayer/playback/player/SimpleMusicPlayer.kt @@ -29,7 +29,15 @@ class SimpleMusicPlayer(val exoPlayer: ExoPlayer) : ForwardingPlayer(exoPlayer) playWhenReady = true } + fun getAudioSessionId(): Int { + return exoPlayer.audioSessionId + } + fun setSkipSilence(skipSilence: Boolean) { exoPlayer.skipSilenceEnabled = skipSilence } + + fun setPauseAtEndOfMediaItems(pauseAtEnd: Boolean) { + exoPlayer.pauseAtEndOfMediaItems = pauseAtEnd + } }