From 0d68ecd3c4495e76f97aef64abf87bef7a397a78 Mon Sep 17 00:00:00 2001 From: Naveen Date: Fri, 8 Mar 2024 06:18:10 +0530 Subject: [PATCH] Use conservative load control Closes https://github.com/FossifyOrg/Gallery/issues/82 --- .../gallery/activities/VideoPlayerActivity.kt | 7 +++++++ .../org/fossify/gallery/fragments/VideoFragment.kt | 14 +++++++++----- .../org/fossify/gallery/helpers/Constants.kt | 3 +++ 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/app/src/main/kotlin/org/fossify/gallery/activities/VideoPlayerActivity.kt b/app/src/main/kotlin/org/fossify/gallery/activities/VideoPlayerActivity.kt index 72f35e957..fa20ae1aa 100644 --- a/app/src/main/kotlin/org/fossify/gallery/activities/VideoPlayerActivity.kt +++ b/app/src/main/kotlin/org/fossify/gallery/activities/VideoPlayerActivity.kt @@ -20,6 +20,7 @@ import androidx.media3.common.util.UnstableApi import androidx.media3.datasource.ContentDataSource import androidx.media3.datasource.DataSource import androidx.media3.datasource.DataSpec +import androidx.media3.exoplayer.DefaultLoadControl import androidx.media3.exoplayer.ExoPlayer import androidx.media3.exoplayer.SeekParameters import androidx.media3.exoplayer.source.DefaultMediaSourceFactory @@ -244,9 +245,15 @@ open class VideoPlayerActivity : SimpleActivity(), SeekBar.OnSeekBarChangeListen val mediaSource: MediaSource = ProgressiveMediaSource.Factory(factory) .createMediaSource(MediaItem.fromUri(fileDataSource.uri!!)) + val loadControl = DefaultLoadControl.Builder() + .setBufferDurationsMs(EXOPLAYER_MIN_BUFFER_MS, EXOPLAYER_MAX_BUFFER_MS, EXOPLAYER_MIN_BUFFER_MS, EXOPLAYER_MIN_BUFFER_MS) + .setPrioritizeTimeOverSizeThresholds(true) + .build() + mExoPlayer = ExoPlayer.Builder(this) .setMediaSourceFactory(DefaultMediaSourceFactory(applicationContext)) .setSeekParameters(SeekParameters.CLOSEST_SYNC) + .setLoadControl(loadControl) .build() .apply { setMediaSource(mediaSource) diff --git a/app/src/main/kotlin/org/fossify/gallery/fragments/VideoFragment.kt b/app/src/main/kotlin/org/fossify/gallery/fragments/VideoFragment.kt index d41c41947..66af31e86 100644 --- a/app/src/main/kotlin/org/fossify/gallery/fragments/VideoFragment.kt +++ b/app/src/main/kotlin/org/fossify/gallery/fragments/VideoFragment.kt @@ -18,6 +18,7 @@ import androidx.media3.datasource.ContentDataSource import androidx.media3.datasource.DataSource import androidx.media3.datasource.DataSpec import androidx.media3.datasource.FileDataSource +import androidx.media3.exoplayer.DefaultLoadControl import androidx.media3.exoplayer.ExoPlayer import androidx.media3.exoplayer.SeekParameters import androidx.media3.exoplayer.source.DefaultMediaSourceFactory @@ -32,10 +33,7 @@ import org.fossify.gallery.databinding.PagerVideoItemBinding import org.fossify.gallery.extensions.config import org.fossify.gallery.extensions.hasNavBar import org.fossify.gallery.extensions.parseFileChannel -import org.fossify.gallery.helpers.Config -import org.fossify.gallery.helpers.FAST_FORWARD_VIDEO_MS -import org.fossify.gallery.helpers.MEDIUM -import org.fossify.gallery.helpers.SHOULD_INIT_FRAGMENT +import org.fossify.gallery.helpers.* import org.fossify.gallery.models.Medium import org.fossify.gallery.views.MediaSideScroll import java.io.File @@ -378,9 +376,15 @@ class VideoFragment : ViewPagerFragment(), TextureView.SurfaceTextureListener, S mPlayOnPrepared = true + val loadControl = DefaultLoadControl.Builder() + .setBufferDurationsMs(EXOPLAYER_MIN_BUFFER_MS, EXOPLAYER_MAX_BUFFER_MS, EXOPLAYER_MIN_BUFFER_MS, EXOPLAYER_MIN_BUFFER_MS) + .setPrioritizeTimeOverSizeThresholds(true) + .build() + mExoPlayer = ExoPlayer.Builder(requireContext()) .setMediaSourceFactory(DefaultMediaSourceFactory(requireContext())) .setSeekParameters(SeekParameters.CLOSEST_SYNC) + .setLoadControl(loadControl) .build() .apply { if (mConfig.loopVideos && listener?.isSlideShowActive() == false) { @@ -408,7 +412,7 @@ class VideoFragment : ViewPagerFragment(), TextureView.SurfaceTextureListener, S override fun onPositionDiscontinuity( oldPosition: Player.PositionInfo, newPosition: Player.PositionInfo, - @Player.DiscontinuityReason reason: Int + @Player.DiscontinuityReason reason: Int, ) { // Reset progress views when video loops. if (reason == Player.DISCONTINUITY_REASON_AUTO_TRANSITION) { diff --git a/app/src/main/kotlin/org/fossify/gallery/helpers/Constants.kt b/app/src/main/kotlin/org/fossify/gallery/helpers/Constants.kt index ef117fc2a..b8189eb78 100644 --- a/app/src/main/kotlin/org/fossify/gallery/helpers/Constants.kt +++ b/app/src/main/kotlin/org/fossify/gallery/helpers/Constants.kt @@ -137,6 +137,9 @@ const val HIDE_SYSTEM_UI_DELAY = 500L const val MAX_PRINT_SIDE_SIZE = 4096 const val FAST_FORWARD_VIDEO_MS = 10000 +const val EXOPLAYER_MIN_BUFFER_MS = 2000 +const val EXOPLAYER_MAX_BUFFER_MS = 8000 + const val DIRECTORY = "directory" const val MEDIUM = "medium" const val PATH = "path"