From 316bb438b369c138aba0513d09848e267474680f Mon Sep 17 00:00:00 2001 From: Naveen Date: Tue, 17 Oct 2023 07:11:24 +0530 Subject: [PATCH 1/4] Handle shuffle when lazily seeking to next/previous media items --- .../playback/player/SimpleMusicPlayer.kt | 39 ++++++++++++------- 1 file changed, 24 insertions(+), 15 deletions(-) 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 200b279a..6541352b 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 @@ -142,25 +142,34 @@ class SimpleMusicPlayer(private val exoPlayer: ExoPlayer) : ForwardingPlayer(exo seekJob?.cancel() seekJob = scope.launch { delay(timeMillis = 400) - if (seekToNextCount > 0 || seekToPreviousCount > 0) { - runOnPlayerThread { - if (currentMediaItem != null) { - if (seekToNextCount > 0) { - seekTo(rotateIndex(currentMediaItemIndex + seekToNextCount), 0) - } - - if (seekToPreviousCount > 0) { - seekTo(rotateIndex(currentMediaItemIndex - seekToPreviousCount), 0) - } - - seekToNextCount = 0 - seekToPreviousCount = 0 - } - } + val seekCount = seekToNextCount - seekToPreviousCount + if (seekCount != 0) { + seekByCount(seekCount) } } } + private fun seekByCount(seekCount: Int) { + runOnPlayerThread { + if (currentMediaItem == null) { + return@runOnPlayerThread + } + + val currentIndex = currentMediaItemIndex + val seekIndex = if (shuffleModeEnabled) { + val shuffledIndex = shuffledMediaItemsIndices.indexOf(currentIndex) + val seekIndex = rotateIndex(shuffledIndex + seekCount) + shuffledMediaItemsIndices.getOrNull(seekIndex) ?: return@runOnPlayerThread + } else { + rotateIndex(currentIndex + seekCount) + } + + seekTo(seekIndex, 0) + seekToNextCount = 0 + seekToPreviousCount = 0 + } + } + private fun rotateIndex(index: Int): Int { val count = mediaItemCount return (index % count + count) % count From 916b7efb900fd354e4a87c011c6138f384018513 Mon Sep 17 00:00:00 2001 From: Naveen Date: Tue, 17 Oct 2023 07:14:39 +0530 Subject: [PATCH 2/4] Reduce seek delay to 300 --- .../musicplayer/playback/player/SimpleMusicPlayer.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 6541352b..95380440 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 @@ -141,7 +141,7 @@ class SimpleMusicPlayer(private val exoPlayer: ExoPlayer) : ForwardingPlayer(exo private fun seekWithDelay() { seekJob?.cancel() seekJob = scope.launch { - delay(timeMillis = 400) + delay(timeMillis = 300) val seekCount = seekToNextCount - seekToPreviousCount if (seekCount != 0) { seekByCount(seekCount) From d541b055a1ac153a179df334acdbdb0777b4818e Mon Sep 17 00:00:00 2001 From: Naveen Date: Tue, 17 Oct 2023 09:03:06 +0530 Subject: [PATCH 3/4] Revert "Update media3 to 1.2.0-alpha02" This reverts commit 7a9c5a16124e397a4a02fa4ca0b18a5f5b61dca6. --- .../playback/player/AudioOnlyRenderersFactory.kt | 9 ++++++++- gradle/libs.versions.toml | 2 +- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/musicplayer/playback/player/AudioOnlyRenderersFactory.kt b/app/src/main/kotlin/com/simplemobiletools/musicplayer/playback/player/AudioOnlyRenderersFactory.kt index 39d31897..b7e72bee 100644 --- a/app/src/main/kotlin/com/simplemobiletools/musicplayer/playback/player/AudioOnlyRenderersFactory.kt +++ b/app/src/main/kotlin/com/simplemobiletools/musicplayer/playback/player/AudioOnlyRenderersFactory.kt @@ -22,7 +22,7 @@ private const val SKIP_SILENCE_THRESHOLD_LEVEL = 16.toShort() @UnstableApi class AudioOnlyRenderersFactory(context: Context) : DefaultRenderersFactory(context) { - override fun buildAudioSink(context: Context, enableFloatOutput: Boolean, enableAudioTrackPlaybackParams: Boolean): AudioSink { + override fun buildAudioSink(context: Context, enableFloatOutput: Boolean, enableAudioTrackPlaybackParams: Boolean, enableOffload: Boolean): AudioSink? { val silenceSkippingAudioProcessor = SilenceSkippingAudioProcessor( SKIP_SILENCE_MINIMUM_DURATION_US, DEFAULT_PADDING_SILENCE_US, @@ -32,6 +32,13 @@ class AudioOnlyRenderersFactory(context: Context) : DefaultRenderersFactory(cont return DefaultAudioSink.Builder(context) .setEnableFloatOutput(enableFloatOutput) .setEnableAudioTrackPlaybackParams(enableAudioTrackPlaybackParams) + .setOffloadMode( + if (enableOffload) { + DefaultAudioSink.OFFLOAD_MODE_ENABLED_GAPLESS_REQUIRED + } else { + DefaultAudioSink.OFFLOAD_MODE_DISABLED + } + ) .setAudioProcessorChain( DefaultAudioSink.DefaultAudioProcessorChain( arrayOf(), diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 8120b98e..c94ee3de 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -18,7 +18,7 @@ eventbus = "3.3.1" lottie = "6.1.0" m3uParser = "1.3.0" media = "1.6.0" -media3 = "1.2.0-alpha02" +media3 = "1.1.1" room = "2.5.2" #Simple Mobile Tools simple-commons = "6a7777d740" From 7371d8b79b11c1fec8139b78f3fb33a5852d2f19 Mon Sep 17 00:00:00 2001 From: Naveen Date: Tue, 17 Oct 2023 09:13:52 +0530 Subject: [PATCH 4/4] Disable skip silence for now Enable when https://github.com/androidx/media/issues/712 is resolved. See https://github.com/SimpleMobileTools/Simple-Music-Player/issues/604 --- .../simplemobiletools/musicplayer/playback/player/Player.kt | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) 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 c3926424..165c832c 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 @@ -64,7 +64,11 @@ private fun PlaybackService.initializePlayer(handleAudioFocus: Boolean, handleAu .build(), handleAudioFocus ) - .setSkipSilenceEnabled(skipSilence) + .setSkipSilenceEnabled( + // TODO: Enable when https://github.com/androidx/media/issues/712 is resolved. + // See https://github.com/SimpleMobileTools/Simple-Music-Player/issues/604 + false //skipSilence + ) .setSeekBackIncrementMs(SEEK_INTERVAL_MS) .setSeekForwardIncrementMs(SEEK_INTERVAL_MS) .setLooper(playerThread.looper)