Skip to content

Commit

Permalink
Add Helper to setup PreloadConfiguration
Browse files Browse the repository at this point in the history
  • Loading branch information
StaehliJ committed Nov 26, 2024
1 parent af0f869 commit 3cc7782
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ import ch.srgssr.pillarbox.demo.shared.di.PlayerModule
import ch.srgssr.pillarbox.player.PillarboxPlayer
import ch.srgssr.pillarbox.player.asset.timeRange.Chapter
import ch.srgssr.pillarbox.player.asset.timeRange.Credit
import ch.srgssr.pillarbox.player.extension.PreloadConfiguration
import ch.srgssr.pillarbox.player.extension.setHandleAudioFocus
import ch.srgssr.pillarbox.player.extension.toRational
import ch.srgssr.pillarbox.player.utils.StringUtil
import kotlinx.coroutines.flow.MutableStateFlow
import kotlin.time.Duration.Companion.seconds

/**
* Simple player view model than handle a PillarboxPlayer [player]
Expand Down Expand Up @@ -72,6 +74,9 @@ class SimplePlayerViewModel(application: Application) : AndroidViewModel(applica
*/
fun playUri(items: List<DemoItem>) {
player.setMediaItems(items.map { it.toMediaItem() })
if (items.size > 1) {
player.preloadConfiguration = PreloadConfiguration(5.seconds)
}
player.prepare()
player.play()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Copyright (c) SRG SSR. All rights reserved.
* License information is available from the LICENSE file.
*/
package ch.srgssr.pillarbox.player.extension

import androidx.media3.common.C
import androidx.media3.exoplayer.ExoPlayer.PreloadConfiguration
import kotlin.time.Duration

/**
* @param targetPreloadDuration The target duration to preload or `null` to disable preloading.
* @return [PreloadConfiguration]
*/
fun PreloadConfiguration(targetPreloadDuration: Duration?): PreloadConfiguration {
return PreloadConfiguration(targetPreloadDuration?.inWholeMicroseconds ?: C.TIME_UNSET)
}

0 comments on commit 3cc7782

Please sign in to comment.