Configurate the notification

It stills need more work, but for now I think is fine.
This commit is contained in:
Relwi 2022-05-26 21:38:25 +02:00
parent 3df5506195
commit f345b142aa
No known key found for this signature in database
GPG Key ID: 3316DC3D260D0163
3 changed files with 34 additions and 22 deletions

View File

@ -1,10 +1,11 @@
package com.github.libretube package com.github.libretube
import android.content.Context import android.content.Context
import android.support.v4.media.session.MediaSessionCompat
import com.github.libretube.obj.Streams import com.github.libretube.obj.Streams
import com.google.android.exoplayer2.ExoPlayer import com.google.android.exoplayer2.ExoPlayer
import com.google.android.exoplayer2.MediaItem import com.google.android.exoplayer2.MediaItem
import com.google.android.exoplayer2.MediaMetadata import com.google.android.exoplayer2.ext.mediasession.MediaSessionConnector
import com.google.android.exoplayer2.ui.PlayerNotificationManager import com.google.android.exoplayer2.ui.PlayerNotificationManager
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
@ -24,42 +25,53 @@ class BackgroundMode {
private var player: ExoPlayer? = null private var player: ExoPlayer? = null
private var playWhenReadyPlayer = true private var playWhenReadyPlayer = true
private var playerNotificationManager: PlayerNotificationManager? = null /**
* The [MediaSessionCompat] for the [response].
*/
private lateinit var mediaSession: MediaSessionCompat
/** /**
* Initializes the [player] player with the [MediaItem]. * 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 lateinit var playerNotification: PlayerNotificationManager
/**
* Initializes the [player] with the [MediaItem].
*/ */
private fun initializePlayer(c: Context) { private fun initializePlayer(c: Context) {
if (player == null) player = ExoPlayer.Builder(c).build() if (player == null) player = ExoPlayer.Builder(c).build()
setMediaItem() setMediaItem(c)
} }
/** /**
* Initializes the [playerNotificationManager] attached to the [player]. * Initializes the [playerNotification] attached to the [player] and shows it.
*/ */
private fun initializePlayerNotification(c: Context) { private fun initializePlayerNotification(c: Context) {
playerNotificationManager = playerNotification = PlayerNotificationManager.Builder(c, 1, "background_mode").build()
PlayerNotificationManager.Builder(c, 1, "background_mode").build() playerNotification.setPlayer(player)
playerNotificationManager?.setPlayer(player) playerNotification.setMediaSessionToken(mediaSession.sessionToken)
} }
/** /**
* Sets the [MediaItem] with the [response] into the [player]. * Sets the [MediaItem] with the [response] into the [player]. Also creates a [MediaSessionConnector]
* with the [mediaSession] and attach it to the [player].
*/ */
private fun setMediaItem() { private fun setMediaItem(c: Context) {
response?.let { response?.let {
// Builds the song metadata val mediaItem = MediaItem.Builder().setUri(it.hls!!).build()
val metaData = MediaMetadata.Builder()
.setTitle(it.title)
.build()
// Builds the song item
val mediaItem = MediaItem.Builder()
.setUri(it.hls!!)
.setMediaMetadata(metaData)
.build()
player?.setMediaItem(mediaItem) player?.setMediaItem(mediaItem)
} }
mediaSession = MediaSessionCompat(c, this.javaClass.name)
mediaSession.isActive = true
mediaSessionConnector = MediaSessionConnector(mediaSession)
mediaSessionConnector.setPlayer(player)
} }
/** /**

View File

@ -15,7 +15,7 @@ class VideoOptionsDialog(private val videoId: String) : DialogFragment() {
/** /**
* List that stores the different menu options. In the future could be add more options here. * List that stores the different menu options. In the future could be add more options here.
*/ */
private val list = listOf("Background mode") private val list = listOf("Play on background")
/** /**
* Dialog that returns a [MaterialAlertDialogBuilder] showing a menu of options. * Dialog that returns a [MaterialAlertDialogBuilder] showing a menu of options.

View File

@ -20,7 +20,7 @@ class myApp : Application() {
// Create the NotificationChannel // Create the NotificationChannel
val name = "Background Mode" val name = "Background Mode"
val descriptionText = "Shows a notification with buttons to control the audio player" val descriptionText = "Shows a notification with buttons to control the audio player"
val importance = NotificationManager.IMPORTANCE_DEFAULT val importance = NotificationManager.IMPORTANCE_LOW
val mChannel = NotificationChannel("background_mode", name, importance) val mChannel = NotificationChannel("background_mode", name, importance)
mChannel.description = descriptionText mChannel.description = descriptionText