diff --git a/app/src/main/java/com/github/libretube/BackgroundMode.kt b/app/src/main/java/com/github/libretube/BackgroundMode.kt index d6d1985cd..7ae8c16df 100644 --- a/app/src/main/java/com/github/libretube/BackgroundMode.kt +++ b/app/src/main/java/com/github/libretube/BackgroundMode.kt @@ -9,10 +9,8 @@ import kotlinx.coroutines.runBlocking /** * Loads the selected video audio in background mode with a notification area. - * - * Needs the [c], necessarily to build the [ExoPlayer] player, and [videoId] to get the video data. */ -class BackgroundMode(private val c: Context, private val videoId: String) { +class BackgroundMode { /** * The response that gets when called the Api. */ @@ -23,40 +21,37 @@ class BackgroundMode(private val c: Context, private val videoId: String) { */ 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) - } - } + private fun initializePlayer(c: Context) { + if (player == null) player = ExoPlayer.Builder(c).build() + setMediaItem() } /** * Releases the [player]. */ private fun releasePlayer() { - player?.let { exoPlayer -> - playbackPosition = exoPlayer.currentPosition - currentItem = exoPlayer.currentMediaItemIndex - playWhenReadyPlayer = exoPlayer.playWhenReady - exoPlayer.release() - } + player?.release() player = null } + /** + * Sets the [MediaItem] with the [response] into the [player]. + */ + private fun setMediaItem() { + response?.let { + val mediaItem = MediaItem.fromUri(response!!.hls!!) + player?.setMediaItem(mediaItem) + } + } + /** * Gets the video data and prepares the [player]. */ - fun playOnBackgroundMode() { + fun playOnBackgroundMode(c: Context, videoId: String) { runBlocking { val job = launch { response = RetrofitInstance.api.getStreams(videoId) @@ -64,13 +59,24 @@ class BackgroundMode(private val c: Context, private val videoId: String) { // Wait until the job is done, to load correctly later in the player job.join() - initializePlayer() + initializePlayer(c) player?.apply { playWhenReady = playWhenReadyPlayer - seekTo(currentItem, playbackPosition) prepare() } } } + + /** + * Creates a singleton of this class, to not create a new [player] every time. + */ + companion object { + private var INSTANCE: BackgroundMode? = null + + fun getInstance(): BackgroundMode { + if (INSTANCE == null) INSTANCE = BackgroundMode() + return INSTANCE!! + } + } } diff --git a/app/src/main/java/com/github/libretube/VideoOptionsDialog.kt b/app/src/main/java/com/github/libretube/VideoOptionsDialog.kt index d04cc03bd..9c69d0426 100644 --- a/app/src/main/java/com/github/libretube/VideoOptionsDialog.kt +++ b/app/src/main/java/com/github/libretube/VideoOptionsDialog.kt @@ -9,8 +9,7 @@ import com.google.android.material.dialog.MaterialAlertDialogBuilder /** * Dialog with different options for a selected video. * - * @param videoId - * The video id. + * Needs the [videoId] to load the content from the right video. */ class VideoOptionsDialog(private val videoId: String) : DialogFragment() { /** @@ -38,7 +37,7 @@ class VideoOptionsDialog(private val videoId: String) : DialogFragment() { when (which) { // This for example will be the "Background mode" option 0 -> { - BackgroundMode(requireContext(), videoId).playOnBackgroundMode() + BackgroundMode.getInstance().playOnBackgroundMode(requireContext(), videoId) } else -> { dialog.dismiss()