Merge pull request #2761 from Bnyro/master

Use the same audio attributes and load control for all players
This commit is contained in:
Bnyro 2023-01-19 17:50:22 +01:00 committed by GitHub
commit e5a370185c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 38 additions and 37 deletions

View File

@ -34,12 +34,10 @@ import com.github.libretube.util.NowPlayingNotification
import com.github.libretube.util.PlayerHelper import com.github.libretube.util.PlayerHelper
import com.github.libretube.util.PlayingQueue import com.github.libretube.util.PlayingQueue
import com.github.libretube.util.PreferenceHelper import com.github.libretube.util.PreferenceHelper
import com.google.android.exoplayer2.C
import com.google.android.exoplayer2.ExoPlayer import com.google.android.exoplayer2.ExoPlayer
import com.google.android.exoplayer2.MediaItem import com.google.android.exoplayer2.MediaItem
import com.google.android.exoplayer2.PlaybackException import com.google.android.exoplayer2.PlaybackException
import com.google.android.exoplayer2.Player import com.google.android.exoplayer2.Player
import com.google.android.exoplayer2.audio.AudioAttributes
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
@ -70,11 +68,6 @@ class BackgroundMode : Service() {
private var player: ExoPlayer? = null private var player: ExoPlayer? = null
private var playWhenReadyPlayer = true private var playWhenReadyPlayer = true
/**
* The [AudioAttributes] handle the audio focus of the [player]
*/
private lateinit var audioAttributes: AudioAttributes
/** /**
* SponsorBlock Segment data * SponsorBlock Segment data
*/ */
@ -246,13 +239,10 @@ class BackgroundMode : Service() {
private fun initializePlayer() { private fun initializePlayer() {
if (player != null) return if (player != null) return
audioAttributes = AudioAttributes.Builder()
.setUsage(C.USAGE_MEDIA)
.setContentType(C.AUDIO_CONTENT_TYPE_MUSIC)
.build()
player = ExoPlayer.Builder(this) player = ExoPlayer.Builder(this)
.setHandleAudioBecomingNoisy(true) .setHandleAudioBecomingNoisy(true)
.setAudioAttributes(audioAttributes, true) .setAudioAttributes(PlayerHelper.getAudioAttributes(), true)
.setLoadControl(PlayerHelper.getLoadControl())
.build() .build()
/** /**

View File

@ -68,6 +68,8 @@ class OfflinePlayerActivity : BaseActivity() {
player = ExoPlayer.Builder(this) player = ExoPlayer.Builder(this)
.setHandleAudioBecomingNoisy(true) .setHandleAudioBecomingNoisy(true)
.setTrackSelector(trackSelector) .setTrackSelector(trackSelector)
.setLoadControl(PlayerHelper.getLoadControl())
.setAudioAttributes(PlayerHelper.getAudioAttributes(), true)
.build().apply { .build().apply {
addListener(object : Player.Listener { addListener(object : Player.Listener {
override fun onEvents(player: Player, events: Player.Events) { override fun onEvents(player: Player, events: Player.Events) {

View File

@ -99,13 +99,11 @@ import com.github.libretube.util.PreferenceHelper
import com.github.libretube.util.SeekbarPreviewListener import com.github.libretube.util.SeekbarPreviewListener
import com.github.libretube.util.TextUtils import com.github.libretube.util.TextUtils
import com.google.android.exoplayer2.C import com.google.android.exoplayer2.C
import com.google.android.exoplayer2.DefaultLoadControl
import com.google.android.exoplayer2.ExoPlayer import com.google.android.exoplayer2.ExoPlayer
import com.google.android.exoplayer2.MediaItem import com.google.android.exoplayer2.MediaItem
import com.google.android.exoplayer2.MediaItem.SubtitleConfiguration import com.google.android.exoplayer2.MediaItem.SubtitleConfiguration
import com.google.android.exoplayer2.PlaybackException import com.google.android.exoplayer2.PlaybackException
import com.google.android.exoplayer2.Player import com.google.android.exoplayer2.Player
import com.google.android.exoplayer2.audio.AudioAttributes
import com.google.android.exoplayer2.ext.cronet.CronetDataSource import com.google.android.exoplayer2.ext.cronet.CronetDataSource
import com.google.android.exoplayer2.source.DefaultMediaSourceFactory import com.google.android.exoplayer2.source.DefaultMediaSourceFactory
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector import com.google.android.exoplayer2.trackselection.DefaultTrackSelector
@ -1335,24 +1333,6 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
cronetDataSourceFactory cronetDataSourceFactory
) )
// handles the audio focus
val audioAttributes = AudioAttributes.Builder()
.setUsage(C.USAGE_MEDIA)
.setContentType(C.AUDIO_CONTENT_TYPE_MOVIE)
.build()
// handles the duration of media to retain in the buffer prior to the current playback position (for fast backward seeking)
val loadControl = DefaultLoadControl.Builder()
// cache the last three minutes
.setBackBuffer(1000 * 60 * 3, true)
.setBufferDurationsMs(
1000 * 10, // exo default is 50s
PlayerHelper.bufferingGoal,
DefaultLoadControl.DEFAULT_BUFFER_FOR_PLAYBACK_MS,
DefaultLoadControl.DEFAULT_BUFFER_FOR_PLAYBACK_AFTER_REBUFFER_MS
)
.build()
// control for the track sources like subtitles and audio source // control for the track sources like subtitles and audio source
trackSelector = DefaultTrackSelector(requireContext()) trackSelector = DefaultTrackSelector(requireContext())
@ -1364,12 +1344,11 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
exoPlayer = ExoPlayer.Builder(requireContext()) exoPlayer = ExoPlayer.Builder(requireContext())
.setMediaSourceFactory(DefaultMediaSourceFactory(dataSourceFactory)) .setMediaSourceFactory(DefaultMediaSourceFactory(dataSourceFactory))
.setLoadControl(loadControl) .setLoadControl(PlayerHelper.getLoadControl())
.setTrackSelector(trackSelector) .setTrackSelector(trackSelector)
.setHandleAudioBecomingNoisy(true) .setHandleAudioBecomingNoisy(true)
.setAudioAttributes(PlayerHelper.getAudioAttributes(), true)
.build() .build()
exoPlayer.setAudioAttributes(audioAttributes, true)
} }
/** /**

View File

@ -16,6 +16,10 @@ import com.github.libretube.api.obj.PipedStream
import com.github.libretube.constants.PreferenceKeys import com.github.libretube.constants.PreferenceKeys
import com.github.libretube.enums.AudioQuality import com.github.libretube.enums.AudioQuality
import com.github.libretube.enums.PlayerEvent import com.github.libretube.enums.PlayerEvent
import com.google.android.exoplayer2.C
import com.google.android.exoplayer2.DefaultLoadControl
import com.google.android.exoplayer2.LoadControl
import com.google.android.exoplayer2.audio.AudioAttributes
import com.google.android.exoplayer2.ui.CaptionStyleCompat import com.google.android.exoplayer2.ui.CaptionStyleCompat
import com.google.android.exoplayer2.video.VideoSize import com.google.android.exoplayer2.video.VideoSize
import kotlin.math.roundToInt import kotlin.math.roundToInt
@ -213,7 +217,7 @@ object PlayerHelper {
"webm" "webm"
) )
val bufferingGoal: Int private val bufferingGoal: Int
get() = PreferenceHelper.getString( get() = PreferenceHelper.getString(
PreferenceKeys.BUFFERING_GOAL, PreferenceKeys.BUFFERING_GOAL,
"50" "50"
@ -332,7 +336,7 @@ object PlayerHelper {
false false
) )
val alternativePiPControls: Boolean private val alternativePiPControls: Boolean
get() = PreferenceHelper.getBoolean( get() = PreferenceHelper.getBoolean(
PreferenceKeys.ALTERNATIVE_PIP_CONTROLS, PreferenceKeys.ALTERNATIVE_PIP_CONTROLS,
false false
@ -429,4 +433,30 @@ object PlayerHelper {
arrayListOf(rewindAction, playPauseAction, forwardAction) arrayListOf(rewindAction, playPauseAction, forwardAction)
} }
} }
/**
* Get the audio attributes to use for the player
*/
fun getAudioAttributes(): AudioAttributes {
return AudioAttributes.Builder()
.setUsage(C.USAGE_MEDIA)
.setContentType(C.AUDIO_CONTENT_TYPE_MOVIE)
.build()
}
/**
* Get the load controls for the player (buffering, etc)
*/
fun getLoadControl(): LoadControl {
return DefaultLoadControl.Builder()
// cache the last three minutes
.setBackBuffer(1000 * 60 * 3, true)
.setBufferDurationsMs(
1000 * 10, // exo default is 50s
bufferingGoal,
DefaultLoadControl.DEFAULT_BUFFER_FOR_PLAYBACK_MS,
DefaultLoadControl.DEFAULT_BUFFER_FOR_PLAYBACK_AFTER_REBUFFER_MS
)
.build()
}
} }