From cb6579362ea34cc47b7c9931a37f892b1af81aea Mon Sep 17 00:00:00 2001 From: Relwi Date: Fri, 20 May 2022 14:20:26 +0200 Subject: [PATCH] Add the notification player Now when you select a video to play it in the background mode, appears a notification (for now does nothing). --- .../com/github/libretube/BackgroundMode.kt | 13 +++++++++ .../main/java/com/github/libretube/myApp.kt | 29 +++++++++++++++++-- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/github/libretube/BackgroundMode.kt b/app/src/main/java/com/github/libretube/BackgroundMode.kt index 7ae8c16df..a8d3efba7 100644 --- a/app/src/main/java/com/github/libretube/BackgroundMode.kt +++ b/app/src/main/java/com/github/libretube/BackgroundMode.kt @@ -4,6 +4,7 @@ import android.content.Context import com.github.libretube.obj.Streams import com.google.android.exoplayer2.ExoPlayer import com.google.android.exoplayer2.MediaItem +import com.google.android.exoplayer2.ui.PlayerNotificationManager import kotlinx.coroutines.launch import kotlinx.coroutines.runBlocking @@ -22,6 +23,8 @@ class BackgroundMode { private var player: ExoPlayer? = null private var playWhenReadyPlayer = true + private var playerNotificationManager: PlayerNotificationManager? = null + /** * Initializes the [player] player with the [MediaItem]. */ @@ -30,6 +33,15 @@ class BackgroundMode { setMediaItem() } + /** + * Initializes the [playerNotificationManager] attached to the [player]. + */ + private fun initializePlayerNotification(c: Context) { + playerNotificationManager = + PlayerNotificationManager.Builder(c, 1, "background_mode").build() + playerNotificationManager?.setPlayer(player) + } + /** * Releases the [player]. */ @@ -60,6 +72,7 @@ class BackgroundMode { job.join() initializePlayer(c) + initializePlayerNotification(c) player?.apply { playWhenReady = playWhenReadyPlayer diff --git a/app/src/main/java/com/github/libretube/myApp.kt b/app/src/main/java/com/github/libretube/myApp.kt index 1f0d7611d..8239f2cfc 100644 --- a/app/src/main/java/com/github/libretube/myApp.kt +++ b/app/src/main/java/com/github/libretube/myApp.kt @@ -1,11 +1,36 @@ package com.github.libretube import android.app.Application +import android.app.NotificationChannel +import android.app.NotificationManager +import android.os.Build class myApp : Application() { + override fun onCreate() { + super.onCreate() + + initializeNotificationChannels() + } + + /** + * Initializes the required [NotificationChannel] for the app. + */ + private fun initializeNotificationChannels() { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + // Create the NotificationChannel + val name = "Background Mode" + val descriptionText = "Shows a notification with buttons to control the audio player" + val importance = NotificationManager.IMPORTANCE_DEFAULT + val mChannel = NotificationChannel("background_mode", name, importance) + mChannel.description = descriptionText + + val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager + notificationManager.createNotificationChannel(mChannel) + } + } companion object { @JvmField - var seekTo : Long? = 0 + var seekTo: Long? = 0 } -} \ No newline at end of file +}