Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: simplify creation of new exo player instances #4861

Merged
merged 1 commit into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,17 @@
import androidx.media3.common.C
import androidx.media3.common.PlaybackParameters
import androidx.media3.common.Tracks
import androidx.media3.datasource.DefaultDataSource
import androidx.media3.datasource.cronet.CronetDataSource
import androidx.media3.exoplayer.DefaultLoadControl
import androidx.media3.exoplayer.ExoPlayer
import androidx.media3.exoplayer.LoadControl
import androidx.media3.exoplayer.source.DefaultMediaSourceFactory
import androidx.media3.exoplayer.trackselection.DefaultTrackSelector
import androidx.media3.ui.CaptionStyleCompat
import com.github.libretube.LibreTubeApp
import com.github.libretube.R
import com.github.libretube.api.CronetHelper
import com.github.libretube.api.obj.ChapterSegment
import com.github.libretube.api.obj.Segment
import com.github.libretube.api.obj.Streams
Expand All @@ -38,6 +42,7 @@
import com.github.libretube.util.TextUtils
import kotlinx.coroutines.runBlocking
import java.util.Locale
import java.util.concurrent.Executors
import kotlin.math.absoluteValue
import kotlin.math.roundToInt

Expand Down Expand Up @@ -433,6 +438,31 @@
.build()
}

/**
* Create a basic player, that is used for all types of playback situations inside the app
*/
@androidx.annotation.OptIn(androidx.media3.common.util.UnstableApi::class)
fun createPlayer(context: Context, trackSelector: DefaultTrackSelector, isBackgroundMode: Boolean): ExoPlayer {

Check failure on line 445 in app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt

View workflow job for this annotation

GitHub Actions / Check Code Quality

[ktlint] reported by reviewdog 🐶 Exceeded max line length (100) Raw Output: app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt:445:1: error: Exceeded max line length (100) (standard:max-line-length)

Check failure on line 445 in app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt

View workflow job for this annotation

GitHub Actions / Check Code Quality

[ktlint] reported by reviewdog 🐶 Parameter should start on a newline Raw Output: app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt:445:22: error: Parameter should start on a newline (standard:parameter-list-wrapping)

Check failure on line 445 in app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt

View workflow job for this annotation

GitHub Actions / Check Code Quality

[ktlint] reported by reviewdog 🐶 Newline expected after opening parenthesis Raw Output: app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt:445:22: error: Newline expected after opening parenthesis (standard:function-signature)

Check failure on line 445 in app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt

View workflow job for this annotation

GitHub Actions / Check Code Quality

[ktlint] reported by reviewdog 🐶 Parameter should start on a newline Raw Output: app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt:445:40: error: Parameter should start on a newline (standard:parameter-list-wrapping)

Check failure on line 445 in app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt

View workflow job for this annotation

GitHub Actions / Check Code Quality

[ktlint] reported by reviewdog 🐶 Parameter should start on a newline Raw Output: app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt:445:40: error: Parameter should start on a newline (standard:function-signature)

Check failure on line 445 in app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt

View workflow job for this annotation

GitHub Actions / Check Code Quality

[ktlint] reported by reviewdog 🐶 Parameter should start on a newline Raw Output: app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt:445:77: error: Parameter should start on a newline (standard:parameter-list-wrapping)

Check failure on line 445 in app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt

View workflow job for this annotation

GitHub Actions / Check Code Quality

[ktlint] reported by reviewdog 🐶 Parameter should start on a newline Raw Output: app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt:445:77: error: Parameter should start on a newline (standard:function-signature)

Check failure on line 445 in app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt

View workflow job for this annotation

GitHub Actions / Check Code Quality

[ktlint] reported by reviewdog 🐶 Missing newline before ")" Raw Output: app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt:445:102: error: Missing newline before ")" (standard:parameter-list-wrapping)

Check failure on line 445 in app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt

View workflow job for this annotation

GitHub Actions / Check Code Quality

[ktlint] reported by reviewdog 🐶 Newline expected before closing parenthesis Raw Output: app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt:445:102: error: Newline expected before closing parenthesis (standard:function-signature)
val cronetDataSourceFactory = CronetDataSource.Factory(
CronetHelper.cronetEngine,
Executors.newCachedThreadPool()
)
val dataSourceFactory = DefaultDataSource.Factory(context, cronetDataSourceFactory)

return ExoPlayer.Builder(context)
.setUsePlatformDiagnostics(false)
.setMediaSourceFactory(DefaultMediaSourceFactory(dataSourceFactory))
.setTrackSelector(trackSelector)
.setHandleAudioBecomingNoisy(true)
.setLoadControl(getLoadControl())
.setAudioAttributes(getAudioAttributes(), true)
.setUsePlatformDiagnostics(false)
.build()
.apply {
loadPlaybackParams(isBackgroundMode)
}
}

/**
* Get the load controls for the player (buffering, etc)
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,7 @@ class OfflinePlayerService : LifecycleService() {
setTrackTypeDisabled(C.TRACK_TYPE_VIDEO, true)
}

player = ExoPlayer.Builder(this)
.setUsePlatformDiagnostics(false)
.setHandleAudioBecomingNoisy(true)
.setAudioAttributes(PlayerHelper.getAudioAttributes(), true)
.setTrackSelector(trackSelector)
.setLoadControl(PlayerHelper.getLoadControl())
.build()
.loadPlaybackParams(isBackgroundMode = true)
player = PlayerHelper.createPlayer(this, trackSelector, true)

val audioItem = downloadWithItem.downloadItems.filter { it.path.exists() }
.firstOrNull { it.type == FileType.AUDIO }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,14 +243,7 @@ class OnlinePlayerService : LifecycleService() {
setTrackTypeDisabled(C.TRACK_TYPE_VIDEO, true)
}

player = ExoPlayer.Builder(this)
.setUsePlatformDiagnostics(false)
.setHandleAudioBecomingNoisy(true)
.setAudioAttributes(PlayerHelper.getAudioAttributes(), true)
.setLoadControl(PlayerHelper.getLoadControl())
.setTrackSelector(trackSelector)
.build()
.loadPlaybackParams(isBackgroundMode = true)
player = PlayerHelper.createPlayer(this, trackSelector, true)

/**
* Listens for changed playbackStates (e.g. pause, end)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,16 @@ import com.github.libretube.enums.FileType
import com.github.libretube.extensions.toAndroidUri
import com.github.libretube.extensions.updateParameters
import com.github.libretube.helpers.PlayerHelper
import com.github.libretube.helpers.PlayerHelper.loadPlaybackParams
import com.github.libretube.helpers.WindowHelper
import com.github.libretube.ui.base.BaseActivity
import com.github.libretube.ui.interfaces.TimeFrameReceiver
import com.github.libretube.ui.listeners.SeekbarPreviewListener
import com.github.libretube.ui.models.PlayerViewModel
import com.github.libretube.util.OfflineTimeFrameReceiver
import kotlin.io.path.exists
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import kotlin.io.path.exists

@androidx.annotation.OptIn(androidx.media3.common.util.UnstableApi::class)
class OfflinePlayerActivity : BaseActivity() {
Expand Down Expand Up @@ -78,39 +77,31 @@ class OfflinePlayerActivity : BaseActivity() {
private fun initializePlayer() {
trackSelector = DefaultTrackSelector(this)

player = ExoPlayer.Builder(this)
.setUsePlatformDiagnostics(false)
.setHandleAudioBecomingNoisy(true)
.setTrackSelector(trackSelector)
.setLoadControl(PlayerHelper.getLoadControl())
.setAudioAttributes(PlayerHelper.getAudioAttributes(), true)
.setUsePlatformDiagnostics(false)
.build().apply {
addListener(object : Player.Listener {
override fun onEvents(player: Player, events: Player.Events) {
super.onEvents(player, events)
// update the displayed duration on changes
playerBinding.duration.text = DateUtils.formatElapsedTime(
player.duration / 1000
)
}
player = PlayerHelper.createPlayer(this, trackSelector, false)

override fun onPlaybackStateChanged(playbackState: Int) {
super.onPlaybackStateChanged(playbackState)
// setup seekbar preview
if (playbackState == Player.STATE_READY) {
binding.player.binding.exoProgress.addListener(
SeekbarPreviewListener(
timeFrameReceiver ?: return,
binding.player.binding,
player.duration
)
)
}
}
})
player.addListener(object : Player.Listener {
override fun onEvents(player: Player, events: Player.Events) {
super.onEvents(player, events)
// update the displayed duration on changes
playerBinding.duration.text = DateUtils.formatElapsedTime(
player.duration / 1000
)
}

override fun onPlaybackStateChanged(playbackState: Int) {
super.onPlaybackStateChanged(playbackState)
// setup seekbar preview
if (playbackState == Player.STATE_READY) {
binding.player.binding.exoProgress.addListener(
SeekbarPreviewListener(
timeFrameReceiver ?: return,
binding.player.binding,
player.duration
)
)
}
}
.loadPlaybackParams()
})

playerView = binding.player
playerView.setShowSubtitleButton(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1405,12 +1405,6 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
}

private fun createExoPlayer() {
val cronetDataSourceFactory = CronetDataSource.Factory(
CronetHelper.cronetEngine,
Executors.newCachedThreadPool()
)
val dataSourceFactory = DefaultDataSource.Factory(requireContext(), cronetDataSourceFactory)

// control for the track sources like subtitles and audio source
trackSelector = DefaultTrackSelector(requireContext())

Expand All @@ -1429,15 +1423,7 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {

PlayerHelper.applyPreferredAudioQuality(requireContext(), trackSelector)

exoPlayer = ExoPlayer.Builder(requireContext())
.setUsePlatformDiagnostics(false)
.setMediaSourceFactory(DefaultMediaSourceFactory(dataSourceFactory))
.setLoadControl(PlayerHelper.getLoadControl())
.setTrackSelector(trackSelector)
.setHandleAudioBecomingNoisy(true)
.setAudioAttributes(PlayerHelper.getAudioAttributes(), true)
.build()
.loadPlaybackParams()
exoPlayer = PlayerHelper.createPlayer(requireContext(), trackSelector, false)
viewModel.player = exoPlayer
}

Expand Down
Loading