Add the notification player

Now when you select a video to play it in the background mode,
appears a notification (for now does nothing).
This commit is contained in:
Relwi 2022-05-20 14:20:26 +02:00
parent 87430dc21e
commit cb6579362e
No known key found for this signature in database
GPG Key ID: 3316DC3D260D0163
2 changed files with 40 additions and 2 deletions

View File

@ -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

View File

@ -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
}
}