From 3cc7782120a0d9b0365f977d8a4b293beb8e661d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joaquim=20St=C3=A4hli?= Date: Tue, 26 Nov 2024 11:26:11 +0100 Subject: [PATCH] Add Helper to setup PreloadConfiguration --- .../demo/ui/player/SimplePlayerViewModel.kt | 5 +++++ .../player/extension/PreloadConfiguration.kt | 17 +++++++++++++++++ 2 files changed, 22 insertions(+) create mode 100644 pillarbox-player/src/main/java/ch/srgssr/pillarbox/player/extension/PreloadConfiguration.kt diff --git a/pillarbox-demo/src/main/java/ch/srgssr/pillarbox/demo/ui/player/SimplePlayerViewModel.kt b/pillarbox-demo/src/main/java/ch/srgssr/pillarbox/demo/ui/player/SimplePlayerViewModel.kt index b7fb24676..adf79fafb 100644 --- a/pillarbox-demo/src/main/java/ch/srgssr/pillarbox/demo/ui/player/SimplePlayerViewModel.kt +++ b/pillarbox-demo/src/main/java/ch/srgssr/pillarbox/demo/ui/player/SimplePlayerViewModel.kt @@ -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] @@ -72,6 +74,9 @@ class SimplePlayerViewModel(application: Application) : AndroidViewModel(applica */ fun playUri(items: List) { player.setMediaItems(items.map { it.toMediaItem() }) + if (items.size > 1) { + player.preloadConfiguration = PreloadConfiguration(5.seconds) + } player.prepare() player.play() } diff --git a/pillarbox-player/src/main/java/ch/srgssr/pillarbox/player/extension/PreloadConfiguration.kt b/pillarbox-player/src/main/java/ch/srgssr/pillarbox/player/extension/PreloadConfiguration.kt new file mode 100644 index 000000000..795b89085 --- /dev/null +++ b/pillarbox-player/src/main/java/ch/srgssr/pillarbox/player/extension/PreloadConfiguration.kt @@ -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) +}