2022-05-06 19:17:02 +05:30
|
|
|
package com.github.libretube
|
|
|
|
|
|
|
|
import android.app.Dialog
|
|
|
|
import android.os.Bundle
|
|
|
|
import android.widget.ArrayAdapter
|
|
|
|
import androidx.fragment.app.DialogFragment
|
2022-05-07 01:45:00 +05:30
|
|
|
import com.github.libretube.obj.Streams
|
|
|
|
import com.google.android.exoplayer2.ExoPlayer
|
|
|
|
import com.google.android.exoplayer2.MediaItem
|
2022-05-06 19:17:02 +05:30
|
|
|
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
2022-05-07 18:17:00 +05:30
|
|
|
import kotlinx.coroutines.launch
|
|
|
|
import kotlinx.coroutines.runBlocking
|
2022-05-06 19:17:02 +05:30
|
|
|
|
2022-05-07 01:45:00 +05:30
|
|
|
/**
|
|
|
|
* Dialog with different options for a selected video.
|
|
|
|
*
|
|
|
|
* @param videoId The video id.
|
|
|
|
*/
|
|
|
|
class VideoOptionsDialog(private val videoId: String) : DialogFragment() {
|
2022-05-06 19:17:02 +05:30
|
|
|
/**
|
2022-05-07 01:45:00 +05:30
|
|
|
* List that stores the different menu options. In the future could be add more options here.
|
2022-05-06 19:17:02 +05:30
|
|
|
*/
|
2022-05-07 01:45:00 +05:30
|
|
|
private val list = listOf("Background mode")
|
2022-05-06 19:17:02 +05:30
|
|
|
|
2022-05-07 01:45:00 +05:30
|
|
|
/**
|
|
|
|
* The response that gets when called the Api.
|
|
|
|
*/
|
2022-05-07 18:17:00 +05:30
|
|
|
private var response: Streams? = null
|
2022-05-07 01:45:00 +05:30
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2022-05-06 19:17:02 +05:30
|
|
|
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
|
|
|
|
return MaterialAlertDialogBuilder(requireContext())
|
2022-05-07 18:17:00 +05:30
|
|
|
.setNegativeButton(R.string.cancel) { dialog, _ ->
|
|
|
|
dialog.dismiss()
|
|
|
|
}
|
2022-05-06 19:17:02 +05:30
|
|
|
.setAdapter(
|
|
|
|
ArrayAdapter(
|
|
|
|
requireContext(),
|
|
|
|
R.layout.video_options_dialog_item,
|
|
|
|
list
|
|
|
|
)
|
2022-05-07 01:45:00 +05:30
|
|
|
) { dialog, which ->
|
2022-05-06 19:17:02 +05:30
|
|
|
// For now, this checks the position of the option with the position that is in the
|
|
|
|
// list. I don't like it, but we will do like this for now.
|
|
|
|
when (which) {
|
|
|
|
// This for example will be the "Background mode" option
|
2022-05-07 01:45:00 +05:30
|
|
|
0 -> {
|
2022-05-07 18:17:00 +05:30
|
|
|
playOnBackgroundMode()
|
2022-05-06 19:17:02 +05:30
|
|
|
}
|
|
|
|
else -> {
|
2022-05-07 01:45:00 +05:30
|
|
|
dialog.dismiss()
|
2022-05-06 19:17:02 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.show()
|
|
|
|
}
|
|
|
|
|
2022-05-07 01:45:00 +05:30
|
|
|
/**
|
2022-05-07 18:17:00 +05:30
|
|
|
* Initializes the [player] player with the [MediaItem].
|
2022-05-07 01:45:00 +05:30
|
|
|
*/
|
|
|
|
private fun initializePlayer() {
|
|
|
|
player = ExoPlayer.Builder(requireContext())
|
|
|
|
.build()
|
|
|
|
.also { exoPlayer ->
|
2022-05-07 18:17:00 +05:30
|
|
|
response?.let {
|
|
|
|
val mediaItem = MediaItem.fromUri(response!!.hls!!)
|
|
|
|
exoPlayer.setMediaItem(mediaItem)
|
|
|
|
}
|
2022-05-07 01:45:00 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-05-07 18:17:00 +05:30
|
|
|
* Releases the [player].
|
2022-05-07 01:45:00 +05:30
|
|
|
*/
|
|
|
|
private fun releasePlayer() {
|
|
|
|
player?.let { exoPlayer ->
|
|
|
|
playbackPosition = exoPlayer.currentPosition
|
|
|
|
currentItem = exoPlayer.currentMediaItemIndex
|
|
|
|
playWhenReady = exoPlayer.playWhenReady
|
|
|
|
exoPlayer.release()
|
|
|
|
}
|
|
|
|
player = null
|
|
|
|
}
|
|
|
|
|
2022-05-07 18:17:00 +05:30
|
|
|
/**
|
|
|
|
* 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()
|
|
|
|
}
|
|
|
|
}
|
2022-05-07 01:45:00 +05:30
|
|
|
|
2022-05-06 19:17:02 +05:30
|
|
|
companion object {
|
|
|
|
const val TAG = "VideoOptionsDialog"
|
|
|
|
}
|
|
|
|
}
|