Use conservative load control

Closes https://github.com/FossifyOrg/Gallery/issues/82
This commit is contained in:
Naveen 2024-03-08 06:18:10 +05:30
parent fba40537ee
commit 0d68ecd3c4
No known key found for this signature in database
GPG key ID: 0E155DAD31671DA3
3 changed files with 19 additions and 5 deletions

View file

@ -20,6 +20,7 @@ import androidx.media3.common.util.UnstableApi
import androidx.media3.datasource.ContentDataSource import androidx.media3.datasource.ContentDataSource
import androidx.media3.datasource.DataSource import androidx.media3.datasource.DataSource
import androidx.media3.datasource.DataSpec import androidx.media3.datasource.DataSpec
import androidx.media3.exoplayer.DefaultLoadControl
import androidx.media3.exoplayer.ExoPlayer import androidx.media3.exoplayer.ExoPlayer
import androidx.media3.exoplayer.SeekParameters import androidx.media3.exoplayer.SeekParameters
import androidx.media3.exoplayer.source.DefaultMediaSourceFactory import androidx.media3.exoplayer.source.DefaultMediaSourceFactory
@ -244,9 +245,15 @@ open class VideoPlayerActivity : SimpleActivity(), SeekBar.OnSeekBarChangeListen
val mediaSource: MediaSource = ProgressiveMediaSource.Factory(factory) val mediaSource: MediaSource = ProgressiveMediaSource.Factory(factory)
.createMediaSource(MediaItem.fromUri(fileDataSource.uri!!)) .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) mExoPlayer = ExoPlayer.Builder(this)
.setMediaSourceFactory(DefaultMediaSourceFactory(applicationContext)) .setMediaSourceFactory(DefaultMediaSourceFactory(applicationContext))
.setSeekParameters(SeekParameters.CLOSEST_SYNC) .setSeekParameters(SeekParameters.CLOSEST_SYNC)
.setLoadControl(loadControl)
.build() .build()
.apply { .apply {
setMediaSource(mediaSource) setMediaSource(mediaSource)

View file

@ -18,6 +18,7 @@ import androidx.media3.datasource.ContentDataSource
import androidx.media3.datasource.DataSource import androidx.media3.datasource.DataSource
import androidx.media3.datasource.DataSpec import androidx.media3.datasource.DataSpec
import androidx.media3.datasource.FileDataSource import androidx.media3.datasource.FileDataSource
import androidx.media3.exoplayer.DefaultLoadControl
import androidx.media3.exoplayer.ExoPlayer import androidx.media3.exoplayer.ExoPlayer
import androidx.media3.exoplayer.SeekParameters import androidx.media3.exoplayer.SeekParameters
import androidx.media3.exoplayer.source.DefaultMediaSourceFactory 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.config
import org.fossify.gallery.extensions.hasNavBar import org.fossify.gallery.extensions.hasNavBar
import org.fossify.gallery.extensions.parseFileChannel import org.fossify.gallery.extensions.parseFileChannel
import org.fossify.gallery.helpers.Config import org.fossify.gallery.helpers.*
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.models.Medium import org.fossify.gallery.models.Medium
import org.fossify.gallery.views.MediaSideScroll import org.fossify.gallery.views.MediaSideScroll
import java.io.File import java.io.File
@ -378,9 +376,15 @@ class VideoFragment : ViewPagerFragment(), TextureView.SurfaceTextureListener, S
mPlayOnPrepared = true 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()) mExoPlayer = ExoPlayer.Builder(requireContext())
.setMediaSourceFactory(DefaultMediaSourceFactory(requireContext())) .setMediaSourceFactory(DefaultMediaSourceFactory(requireContext()))
.setSeekParameters(SeekParameters.CLOSEST_SYNC) .setSeekParameters(SeekParameters.CLOSEST_SYNC)
.setLoadControl(loadControl)
.build() .build()
.apply { .apply {
if (mConfig.loopVideos && listener?.isSlideShowActive() == false) { if (mConfig.loopVideos && listener?.isSlideShowActive() == false) {
@ -408,7 +412,7 @@ class VideoFragment : ViewPagerFragment(), TextureView.SurfaceTextureListener, S
override fun onPositionDiscontinuity( override fun onPositionDiscontinuity(
oldPosition: Player.PositionInfo, oldPosition: Player.PositionInfo,
newPosition: Player.PositionInfo, newPosition: Player.PositionInfo,
@Player.DiscontinuityReason reason: Int @Player.DiscontinuityReason reason: Int,
) { ) {
// Reset progress views when video loops. // Reset progress views when video loops.
if (reason == Player.DISCONTINUITY_REASON_AUTO_TRANSITION) { if (reason == Player.DISCONTINUITY_REASON_AUTO_TRANSITION) {

View file

@ -137,6 +137,9 @@ const val HIDE_SYSTEM_UI_DELAY = 500L
const val MAX_PRINT_SIDE_SIZE = 4096 const val MAX_PRINT_SIDE_SIZE = 4096
const val FAST_FORWARD_VIDEO_MS = 10000 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 DIRECTORY = "directory"
const val MEDIUM = "medium" const val MEDIUM = "medium"
const val PATH = "path" const val PATH = "path"