mirror of
https://github.com/libre-tube/LibreTube.git
synced 2025-04-29 08:20:32 +05:30
Litte refactor
Move the background mode logical to another class for better code distribution.
This commit is contained in:
parent
970d1246ab
commit
713d9cfe5b
80
app/src/main/java/com/github/libretube/BackgroundMode.kt
Normal file
80
app/src/main/java/com/github/libretube/BackgroundMode.kt
Normal file
@ -0,0 +1,80 @@
|
||||
package com.github.libretube
|
||||
|
||||
import android.content.Context
|
||||
import com.github.libretube.obj.Streams
|
||||
import com.google.android.exoplayer2.ExoPlayer
|
||||
import com.google.android.exoplayer2.MediaItem
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.runBlocking
|
||||
|
||||
/**
|
||||
* Loads the selected video audio in background mode with a notification area.
|
||||
*
|
||||
* @param c
|
||||
* The context, necessarily to build the [ExoPlayer] player.
|
||||
*
|
||||
* @param videoId
|
||||
* The video id to get the video data.
|
||||
*/
|
||||
class BackgroundMode(private val c: Context, private val videoId: String) {
|
||||
/**
|
||||
* The response that gets when called the Api.
|
||||
*/
|
||||
private var response: Streams? = null
|
||||
|
||||
/**
|
||||
* The [ExoPlayer] player. Followed tutorial [here](https://developer.android.com/codelabs/exoplayer-intro)
|
||||
*/
|
||||
private var player: ExoPlayer? = null
|
||||
private var playWhenReadyPlayer = true
|
||||
private var currentItem = 0
|
||||
private var playbackPosition = 0L
|
||||
|
||||
/**
|
||||
* Initializes the [player] player with the [MediaItem].
|
||||
*/
|
||||
private fun initializePlayer() {
|
||||
player = ExoPlayer.Builder(c)
|
||||
.build()
|
||||
.also { exoPlayer ->
|
||||
response?.let {
|
||||
val mediaItem = MediaItem.fromUri(response!!.hls!!)
|
||||
exoPlayer.setMediaItem(mediaItem)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Releases the [player].
|
||||
*/
|
||||
private fun releasePlayer() {
|
||||
player?.let { exoPlayer ->
|
||||
playbackPosition = exoPlayer.currentPosition
|
||||
currentItem = exoPlayer.currentMediaItemIndex
|
||||
playWhenReadyPlayer = exoPlayer.playWhenReady
|
||||
exoPlayer.release()
|
||||
}
|
||||
player = null
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the video data and prepares the [player].
|
||||
*/
|
||||
fun playOnBackgroundMode() {
|
||||
runBlocking {
|
||||
val job = launch {
|
||||
response = RetrofitInstance.api.getStreams(videoId)
|
||||
}
|
||||
// Wait until the job is done, to load correctly later in the player
|
||||
job.join()
|
||||
|
||||
initializePlayer()
|
||||
|
||||
player?.apply {
|
||||
playWhenReady = playWhenReadyPlayer
|
||||
seekTo(currentItem, playbackPosition)
|
||||
prepare()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -4,17 +4,13 @@ import android.app.Dialog
|
||||
import android.os.Bundle
|
||||
import android.widget.ArrayAdapter
|
||||
import androidx.fragment.app.DialogFragment
|
||||
import com.github.libretube.obj.Streams
|
||||
import com.google.android.exoplayer2.ExoPlayer
|
||||
import com.google.android.exoplayer2.MediaItem
|
||||
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.runBlocking
|
||||
|
||||
/**
|
||||
* Dialog with different options for a selected video.
|
||||
*
|
||||
* @param videoId The video id.
|
||||
* @param videoId
|
||||
* The video id.
|
||||
*/
|
||||
class VideoOptionsDialog(private val videoId: String) : DialogFragment() {
|
||||
/**
|
||||
@ -22,19 +18,6 @@ class VideoOptionsDialog(private val videoId: String) : DialogFragment() {
|
||||
*/
|
||||
private val list = listOf("Background mode")
|
||||
|
||||
/**
|
||||
* The response that gets when called the Api.
|
||||
*/
|
||||
private var response: Streams? = null
|
||||
|
||||
/**
|
||||
* The [ExoPlayer] player. Followed tutorial [here](https://developer.android.com/codelabs/exoplayer-intro)
|
||||
*/
|
||||
private var player: ExoPlayer? = null
|
||||
private var playWhenReady = true
|
||||
private var currentItem = 0
|
||||
private var playbackPosition = 0L
|
||||
|
||||
/**
|
||||
* Dialog that returns a [MaterialAlertDialogBuilder] showing a menu of options.
|
||||
*/
|
||||
@ -55,7 +38,7 @@ class VideoOptionsDialog(private val videoId: String) : DialogFragment() {
|
||||
when (which) {
|
||||
// This for example will be the "Background mode" option
|
||||
0 -> {
|
||||
playOnBackgroundMode()
|
||||
BackgroundMode(requireContext(), videoId).playOnBackgroundMode()
|
||||
}
|
||||
else -> {
|
||||
dialog.dismiss()
|
||||
@ -65,52 +48,6 @@ class VideoOptionsDialog(private val videoId: String) : DialogFragment() {
|
||||
.show()
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes the [player] player with the [MediaItem].
|
||||
*/
|
||||
private fun initializePlayer() {
|
||||
player = ExoPlayer.Builder(requireContext())
|
||||
.build()
|
||||
.also { exoPlayer ->
|
||||
response?.let {
|
||||
val mediaItem = MediaItem.fromUri(response!!.hls!!)
|
||||
exoPlayer.setMediaItem(mediaItem)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Releases the [player].
|
||||
*/
|
||||
private fun releasePlayer() {
|
||||
player?.let { exoPlayer ->
|
||||
playbackPosition = exoPlayer.currentPosition
|
||||
currentItem = exoPlayer.currentMediaItemIndex
|
||||
playWhenReady = exoPlayer.playWhenReady
|
||||
exoPlayer.release()
|
||||
}
|
||||
player = null
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the video data and prepares the [player].
|
||||
*/
|
||||
private fun playOnBackgroundMode() {
|
||||
runBlocking {
|
||||
val job = launch {
|
||||
response = RetrofitInstance.api.getStreams(videoId)
|
||||
}
|
||||
// Wait until the job is done, to load correctly later in the player
|
||||
job.join()
|
||||
|
||||
initializePlayer()
|
||||
|
||||
player?.playWhenReady = playWhenReady
|
||||
player?.seekTo(currentItem, playbackPosition)
|
||||
player?.prepare()
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
const val TAG = "VideoOptionsDialog"
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user