Skip to content

Commit

Permalink
Merge pull request #616 from naveensingh/fix_shuffle_and_other_issues
Browse files Browse the repository at this point in the history
Fix shuffle and other issues
  • Loading branch information
tibbi authored Oct 17, 2023
2 parents 4399b85 + 7371d8b commit 021666c
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,26 +141,35 @@ class SimpleMusicPlayer(private val exoPlayer: ExoPlayer) : ForwardingPlayer(exo
private fun seekWithDelay() {
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
}
}
delay(timeMillis = 300)
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
Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 021666c

Please sign in to comment.