From 3399e4a8a820691f88e59c2da7925583ea7ed7cc Mon Sep 17 00:00:00 2001 From: Bnyro Date: Sun, 7 Aug 2022 19:01:03 +0200 Subject: [PATCH] use proper way --- .../libretube/services/BackgroundMode.kt | 38 +++++++------------ .../libretube/util/NowPlayingNotification.kt | 34 ++++++++++++++--- 2 files changed, 42 insertions(+), 30 deletions(-) diff --git a/app/src/main/java/com/github/libretube/services/BackgroundMode.kt b/app/src/main/java/com/github/libretube/services/BackgroundMode.kt index 38481014b..e6ee2dcd7 100644 --- a/app/src/main/java/com/github/libretube/services/BackgroundMode.kt +++ b/app/src/main/java/com/github/libretube/services/BackgroundMode.kt @@ -9,7 +9,6 @@ import android.os.Build import android.os.Handler import android.os.IBinder import android.os.Looper -import android.support.v4.media.session.MediaSessionCompat import com.fasterxml.jackson.databind.ObjectMapper import com.github.libretube.BACKGROUND_CHANNEL_ID import com.github.libretube.PLAYER_NOTIFICATION_ID @@ -54,16 +53,6 @@ class BackgroundMode : Service() { private var player: ExoPlayer? = null private var playWhenReadyPlayer = true - /** - * The [MediaSessionCompat] for the [response]. - */ - private lateinit var mediaSession: MediaSessionCompat - - /** - * The [MediaSessionConnector] to connect with the [mediaSession] and implement it with the [player]. - */ - private lateinit var mediaSessionConnector: MediaSessionConnector - /** * The [AudioAttributes] handle the audio focus of the [player] */ @@ -104,12 +93,17 @@ class BackgroundMode : Service() { * Initializes the [player] with the [MediaItem]. */ override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { - // get the intent arguments - videoId = intent?.getStringExtra("videoId")!! - val position = intent.getLongExtra("position", 0L) + try { + // get the intent arguments + videoId = intent?.getStringExtra("videoId")!! + val position = intent.getLongExtra("position", 0L) - // play the audio in the background - playAudio(videoId, position) + // play the audio in the background + playAudio(videoId, position) + } catch (e: Exception) { + stopForeground(true) + stopSelf() + } return super.onStartCommand(intent, flags, startId) } @@ -131,8 +125,10 @@ class BackgroundMode : Service() { setMediaItem() // create the notification - nowPlayingNotification = NowPlayingNotification(this@BackgroundMode, player!!) - nowPlayingNotification.initializePlayerNotification(mediaSession, response!!) + if (!this@BackgroundMode::nowPlayingNotification.isInitialized) { + nowPlayingNotification = NowPlayingNotification(this@BackgroundMode, player!!) + } + nowPlayingNotification.updatePlayerNotification(response!!) player?.apply { playWhenReady = playWhenReadyPlayer @@ -208,12 +204,6 @@ class BackgroundMode : Service() { val mediaItem = MediaItem.Builder().setUri(it.hls!!).build() player?.setMediaItem(mediaItem) } - - mediaSession = MediaSessionCompat(this, this.javaClass.name) - mediaSession.isActive = true - - mediaSessionConnector = MediaSessionConnector(mediaSession) - mediaSessionConnector.setPlayer(player) } /** diff --git a/app/src/main/java/com/github/libretube/util/NowPlayingNotification.kt b/app/src/main/java/com/github/libretube/util/NowPlayingNotification.kt index f9a8638ae..83b090ed4 100644 --- a/app/src/main/java/com/github/libretube/util/NowPlayingNotification.kt +++ b/app/src/main/java/com/github/libretube/util/NowPlayingNotification.kt @@ -12,6 +12,7 @@ import com.github.libretube.activities.MainActivity import com.github.libretube.obj.Streams import com.google.android.exoplayer2.ExoPlayer import com.google.android.exoplayer2.Player +import com.google.android.exoplayer2.ext.mediasession.MediaSessionConnector import com.google.android.exoplayer2.ui.PlayerNotificationManager import java.net.URL @@ -21,15 +22,21 @@ class NowPlayingNotification( ) { private var streams: Streams? = null + /** + * The [MediaSessionCompat] for the [streams]. + */ + private lateinit var mediaSession: MediaSessionCompat + + /** + * The [MediaSessionConnector] to connect with the [mediaSession] and implement it with the [player]. + */ + private lateinit var mediaSessionConnector: MediaSessionConnector + /** * The [PlayerNotificationManager] to load the [mediaSession] content on it. */ private var playerNotification: PlayerNotificationManager? = null - private fun setStreams(streams: Streams) { - this.streams = streams - } - /** * The [DescriptionAdapter] is used to show title, uploaderName and thumbnail of the video in the notification * Basic example [here](https://github.com/AnthonyMarkD/AudioPlayerSampleTest) @@ -109,14 +116,29 @@ class NowPlayingNotification( } } + private fun createMediaSession() { + if (this::mediaSession.isInitialized) return + mediaSession = MediaSessionCompat(context, this.javaClass.name) + mediaSession.isActive = true + + mediaSessionConnector = MediaSessionConnector(mediaSession) + mediaSessionConnector.setPlayer(player) + } + /** * Initializes the [playerNotification] attached to the [player] and shows it. */ - fun initializePlayerNotification( - mediaSession: MediaSessionCompat, + fun updatePlayerNotification( streams: Streams ) { this.streams = streams + if (playerNotification == null) { + createMediaSession() + createNotification() + } + } + + private fun createNotification() { playerNotification = PlayerNotificationManager .Builder(context, PLAYER_NOTIFICATION_ID, BACKGROUND_CHANNEL_ID) // set the description of the notification