From d4f3e9cb9e945e51658e30f684918bb46f33c2fb Mon Sep 17 00:00:00 2001 From: Naveen Date: Mon, 4 Sep 2023 12:36:52 +0530 Subject: [PATCH] Tune skip silence threshold This ensures we don't skip any short pauses in the middle of a track --- .../player/AudioOnlyRenderersFactory.kt | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) 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 b0da8b26..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 @@ -3,17 +3,52 @@ package com.simplemobiletools.musicplayer.playback.player import android.content.Context import android.os.Handler import android.os.Looper +import androidx.media3.common.audio.SonicAudioProcessor import androidx.media3.common.util.UnstableApi import androidx.media3.exoplayer.DefaultRenderersFactory import androidx.media3.exoplayer.Renderer +import androidx.media3.exoplayer.audio.AudioSink +import androidx.media3.exoplayer.audio.DefaultAudioSink +import androidx.media3.exoplayer.audio.SilenceSkippingAudioProcessor +import androidx.media3.exoplayer.audio.SilenceSkippingAudioProcessor.DEFAULT_PADDING_SILENCE_US import androidx.media3.exoplayer.mediacodec.MediaCodecSelector import androidx.media3.exoplayer.metadata.MetadataOutput import androidx.media3.exoplayer.text.TextOutput import androidx.media3.exoplayer.video.VideoRendererEventListener +private const val SKIP_SILENCE_MINIMUM_DURATION_US = 300000L +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, enableOffload: Boolean): AudioSink? { + val silenceSkippingAudioProcessor = SilenceSkippingAudioProcessor( + SKIP_SILENCE_MINIMUM_DURATION_US, + DEFAULT_PADDING_SILENCE_US, + SKIP_SILENCE_THRESHOLD_LEVEL + ) + + 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(), + silenceSkippingAudioProcessor, + SonicAudioProcessor() + ) + ) + .build() + } + override fun buildVideoRenderers( context: Context, extensionRendererMode: Int,