11package com.github.libretube.helpers
22
33import android.app.Activity
4+ import android.app.ActivityManager
45import android.content.Context
56import android.content.Intent
67import android.content.pm.ActivityInfo
@@ -512,7 +513,7 @@ object PlayerHelper {
512513 * Create a basic player, that is used for all types of playback situations inside the app
513514 */
514515 @OptIn(androidx.media3.common.util.UnstableApi ::class )
515- fun createPlayer (context : Context , trackSelector : DefaultTrackSelector , ): ExoPlayer {
516+ fun createPlayer (context : Context , trackSelector : DefaultTrackSelector ): ExoPlayer {
516517 val dataSourceFactory = DefaultDataSource .Factory (context)
517518 val audioAttributes = AudioAttributes .Builder ()
518519 .setUsage(C .USAGE_MEDIA )
@@ -532,22 +533,38 @@ object PlayerHelper {
532533 }
533534 }
534535
536+
537+ /* *
538+ * Returns the recommended duration that should be buffer in milliseconds, based on the available memory.
539+ */
540+ private fun getRecommendedBufferMs (): Int {
541+ val activityManager =
542+ LibreTubeApp .instance.getSystemService(Context .ACTIVITY_SERVICE ) as ActivityManager
543+ val memInfo = ActivityManager .MemoryInfo ().also { memoryInfo ->
544+ activityManager.getMemoryInfo(memoryInfo)
545+ }
546+ // device memory in MB
547+ val availableMemory = memInfo.totalMem / (1024 * 1024 )
548+ return when {
549+ availableMemory >= 8192 -> 1000 * 450
550+ availableMemory >= 4096 -> 1000 * 300
551+ availableMemory > 2048 -> 1000 * 60 * 2
552+ else -> MINIMUM_BUFFER_DURATION
553+ }
554+ }
555+
535556 /* *
536557 * Get the load controls for the player (buffering, etc)
537558 */
538559 @OptIn(androidx.media3.common.util.UnstableApi ::class )
539- fun getLoadControl (): LoadControl {
540- return DefaultLoadControl .Builder ()
541- // cache the last three minutes
542- .setBackBuffer(1000 * 60 * 3 , true )
543- .setBufferDurationsMs(
544- MINIMUM_BUFFER_DURATION ,
545- max(bufferingGoal, MINIMUM_BUFFER_DURATION ),
546- DefaultLoadControl .DEFAULT_BUFFER_FOR_PLAYBACK_MS ,
547- DefaultLoadControl .DEFAULT_BUFFER_FOR_PLAYBACK_AFTER_REBUFFER_MS
548- )
549- .build()
550- }
560+ fun getLoadControl (): LoadControl = DefaultLoadControl .Builder ()
561+ // cache the last three minutes
562+ .setBackBuffer(1000 * 60 * 3 , true ).setBufferDurationsMs(
563+ MINIMUM_BUFFER_DURATION ,
564+ getRecommendedBufferMs(),
565+ DefaultLoadControl .DEFAULT_BUFFER_FOR_PLAYBACK_MS ,
566+ DefaultLoadControl .DEFAULT_BUFFER_FOR_PLAYBACK_AFTER_REBUFFER_MS
567+ ).build()
551568
552569 /* *
553570 * Load playback parameters such as speed and skip silence
0 commit comments