Basic behavior on the background mode

For now I can't get properly the Stream with the data, but with
a hardcoded url it works.
This commit is contained in:
Relwi 2022-05-06 22:15:00 +02:00
parent b8933b61ae
commit bcfa84a08a
No known key found for this signature in database
GPG Key ID: 3316DC3D260D0163
2 changed files with 68 additions and 8 deletions

View File

@ -4,14 +4,39 @@ import android.app.Dialog
import android.os.Bundle import android.os.Bundle
import android.widget.ArrayAdapter import android.widget.ArrayAdapter
import androidx.fragment.app.DialogFragment import androidx.fragment.app.DialogFragment
import androidx.lifecycle.lifecycleScope
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 com.google.android.material.dialog.MaterialAlertDialogBuilder
class VideoOptionsDialog : DialogFragment() { /**
/** * Dialog with different options for a selected video.
* List that stores the different menu options. *
* @param videoId The video id.
*/ */
private val list = arrayListOf("Background mode") class VideoOptionsDialog(private val videoId: String) : DialogFragment() {
/**
* List that stores the different menu options. In the future could be add more options here.
*/
private val list = listOf("Background mode")
/**
* The response that gets when called the Api.
*/
private lateinit var response: Streams
/**
* 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.
*/
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return MaterialAlertDialogBuilder(requireContext()) return MaterialAlertDialogBuilder(requireContext())
.setAdapter( .setAdapter(
@ -20,22 +45,56 @@ class VideoOptionsDialog : DialogFragment() {
R.layout.video_options_dialog_item, R.layout.video_options_dialog_item,
list list
) )
) { _, which -> ) { dialog, which ->
// For now, this checks the position of the option with the position that is in the // 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. // list. I don't like it, but we will do like this for now.
when (which) { when (which) {
// This for example will be the "Background mode" option // This for example will be the "Background mode" option
1 -> { 0 -> {
TODO("Implement the background mode option") lifecycleScope.launchWhenCreated {
// FIXME: For some reason I can't get the response
response = RetrofitInstance.api.getStreams(videoId)
initializePlayer()
player?.playWhenReady = playWhenReady
player?.seekTo(currentItem, playbackPosition)
player?.prepare()
}
} }
else -> { else -> {
this.dismiss() dialog.dismiss()
} }
} }
} }
.show() .show()
} }
/**
* Initializes the [Exoplayer] player with the [MediaItem].
*/
private fun initializePlayer() {
player = ExoPlayer.Builder(requireContext())
.build()
.also { exoPlayer ->
val mediaItem = MediaItem.fromUri(response.hls!!)
exoPlayer.setMediaItem(mediaItem)
}
}
/**
* Releases the [ExoPlayer].
*/
private fun releasePlayer() {
player?.let { exoPlayer ->
playbackPosition = exoPlayer.currentPosition
currentItem = exoPlayer.currentMediaItemIndex
playWhenReady = exoPlayer.playWhenReady
exoPlayer.release()
}
player = null
}
companion object { companion object {
const val TAG = "VideoOptionsDialog" const val TAG = "VideoOptionsDialog"
} }

View File

@ -78,7 +78,8 @@ class TrendingAdapter(
.commitNow() .commitNow()
} }
holder.v.setOnLongClickListener { holder.v.setOnLongClickListener {
VideoOptionsDialog().show(childFragmentManager, VideoOptionsDialog.TAG) val videoId = trending.url!!.replace("/watch?v=", "")
VideoOptionsDialog(videoId).show(childFragmentManager, VideoOptionsDialog.TAG)
true true
} }
} }