From 8f758566f128009fd82d4d881742ad1626b80116 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Thu, 30 Jun 2022 16:02:55 +0200 Subject: [PATCH] autoplay in background mode --- .../com/github/libretube/BackgroundMode.kt | 53 +++++++++++++++++-- .../libretube/dialogs/VideoOptionsDialog.kt | 2 +- .../libretube/fragments/PlayerFragment.kt | 2 +- .../libretube/util/DescriptionAdapter.kt | 4 +- 4 files changed, 53 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/com/github/libretube/BackgroundMode.kt b/app/src/main/java/com/github/libretube/BackgroundMode.kt index 79ff8a319..a95a8801d 100644 --- a/app/src/main/java/com/github/libretube/BackgroundMode.kt +++ b/app/src/main/java/com/github/libretube/BackgroundMode.kt @@ -1,13 +1,16 @@ package com.github.libretube +import android.app.NotificationManager import android.content.Context import android.support.v4.media.session.MediaSessionCompat import com.github.libretube.obj.Streams import com.github.libretube.util.DescriptionAdapter +import com.github.libretube.util.PreferenceHelper import com.github.libretube.util.RetrofitInstance import com.google.android.exoplayer2.C import com.google.android.exoplayer2.ExoPlayer import com.google.android.exoplayer2.MediaItem +import com.google.android.exoplayer2.Player import com.google.android.exoplayer2.audio.AudioAttributes import com.google.android.exoplayer2.ext.mediasession.MediaSessionConnector import com.google.android.exoplayer2.ui.PlayerNotificationManager @@ -42,7 +45,7 @@ class BackgroundMode { /** * The [PlayerNotificationManager] to load the [mediaSession] content on it. */ - private lateinit var playerNotification: PlayerNotificationManager + private var playerNotification: PlayerNotificationManager? = null /** * The [AudioAttributes] handle the audio focus of the [player] @@ -63,9 +66,45 @@ class BackgroundMode { .setAudioAttributes(audioAttributes, true) .build() } + + /** + * Listens for changed playbackStates (e.g. pause, end) + * Plays the next video when the current one ended + */ + player!!.addListener(object : Player.Listener { + override fun onPlaybackStateChanged(@Player.State state: Int) { + val autoplay = PreferenceHelper.getBoolean(c, "autoplay", false) + if (state == Player.STATE_ENDED) { + if (autoplay) playNextVideo(c) + } + } + }) setMediaItem(c) } + /** + * Plays the first related video to the current (used when the playback of the current video ended) + */ + private fun playNextVideo(c: Context) { + if (response!!.relatedStreams!!.isNotEmpty()) { + val videoId = response!! + .relatedStreams!![0].url!! + .replace("/watch?v=", "") + + // destroy old player and its notification + playerNotification = null + player = null + + // kill old notification + val notificationManager = c.getSystemService(Context.NOTIFICATION_SERVICE) + as NotificationManager + notificationManager.cancel(1) + + // play new video on background + playOnBackgroundMode(c, videoId) + } + } + /** * Initializes the [playerNotification] attached to the [player] and shows it. */ @@ -82,10 +121,12 @@ class BackgroundMode { ) ) .build() - playerNotification.apply { + playerNotification?.apply { setPlayer(player) - setUsePreviousAction(false) setUseNextAction(false) + setUsePreviousAction(false) + setUseStopAction(true) + setColorized(true) setMediaSessionToken(mediaSession.sessionToken) } } @@ -110,7 +151,11 @@ class BackgroundMode { /** * Gets the video data and prepares the [player]. */ - fun playOnBackgroundMode(c: Context, videoId: String, seekToPosition: Long) { + fun playOnBackgroundMode( + c: Context, + videoId: String, + seekToPosition: Long = 0 + ) { runBlocking { val job = launch { response = RetrofitInstance.api.getStreams(videoId) diff --git a/app/src/main/java/com/github/libretube/dialogs/VideoOptionsDialog.kt b/app/src/main/java/com/github/libretube/dialogs/VideoOptionsDialog.kt index 9271af500..da87f5551 100644 --- a/app/src/main/java/com/github/libretube/dialogs/VideoOptionsDialog.kt +++ b/app/src/main/java/com/github/libretube/dialogs/VideoOptionsDialog.kt @@ -47,7 +47,7 @@ class VideoOptionsDialog(private val videoId: String, context: Context) : Dialog // This for example will be the "Background mode" option 0 -> { BackgroundMode.getInstance() - .playOnBackgroundMode(requireContext(), videoId, 0) + .playOnBackgroundMode(requireContext(), videoId) } // Add Video to Playlist Dialog 1 -> { diff --git a/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt b/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt index 779203258..55b6cbd3b 100644 --- a/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt +++ b/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt @@ -1006,8 +1006,8 @@ class PlayerFragment : Fragment() { playerNotification.apply { setPlayer(exoPlayer) - setUseNextAction(false) setUsePreviousAction(false) + setUseStopAction(true) setMediaSessionToken(mediaSession.sessionToken) } } diff --git a/app/src/main/java/com/github/libretube/util/DescriptionAdapter.kt b/app/src/main/java/com/github/libretube/util/DescriptionAdapter.kt index 8f81b6fad..a7c9c1ec9 100644 --- a/app/src/main/java/com/github/libretube/util/DescriptionAdapter.kt +++ b/app/src/main/java/com/github/libretube/util/DescriptionAdapter.kt @@ -83,8 +83,8 @@ class DescriptionAdapter( return try { val resizedBitmap = Bitmap.createScaledBitmap( bitmap, - 1080, - 1080, + bitmap.width, + bitmap.width, false ) resizedBitmap