diff --git a/app/src/main/java/com/github/libretube/BackgroundMode.kt b/app/src/main/java/com/github/libretube/BackgroundMode.kt index de7b84f80..8299d6ae6 100644 --- a/app/src/main/java/com/github/libretube/BackgroundMode.kt +++ b/app/src/main/java/com/github/libretube/BackgroundMode.kt @@ -1,10 +1,11 @@ package com.github.libretube import android.content.Context +import android.support.v4.media.session.MediaSessionCompat import com.github.libretube.obj.Streams import com.google.android.exoplayer2.ExoPlayer 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 kotlinx.coroutines.launch import kotlinx.coroutines.runBlocking @@ -24,42 +25,53 @@ class BackgroundMode { private var player: ExoPlayer? = null 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) { 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) { - playerNotificationManager = - PlayerNotificationManager.Builder(c, 1, "background_mode").build() - playerNotificationManager?.setPlayer(player) + playerNotification = PlayerNotificationManager.Builder(c, 1, "background_mode").build() + playerNotification.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 { - // Builds the song metadata - val metaData = MediaMetadata.Builder() - .setTitle(it.title) - .build() - // Builds the song item - val mediaItem = MediaItem.Builder() - .setUri(it.hls!!) - .setMediaMetadata(metaData) - .build() - + val mediaItem = MediaItem.Builder().setUri(it.hls!!).build() player?.setMediaItem(mediaItem) } + + mediaSession = MediaSessionCompat(c, this.javaClass.name) + mediaSession.isActive = true + + mediaSessionConnector = MediaSessionConnector(mediaSession) + mediaSessionConnector.setPlayer(player) } /** diff --git a/app/src/main/java/com/github/libretube/VideoOptionsDialog.kt b/app/src/main/java/com/github/libretube/VideoOptionsDialog.kt index 9c69d0426..854702854 100644 --- a/app/src/main/java/com/github/libretube/VideoOptionsDialog.kt +++ b/app/src/main/java/com/github/libretube/VideoOptionsDialog.kt @@ -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. */ - private val list = listOf("Background mode") + private val list = listOf("Play on background") /** * Dialog that returns a [MaterialAlertDialogBuilder] showing a menu of options. diff --git a/app/src/main/java/com/github/libretube/myApp.kt b/app/src/main/java/com/github/libretube/myApp.kt index 8239f2cfc..d5eb50444 100644 --- a/app/src/main/java/com/github/libretube/myApp.kt +++ b/app/src/main/java/com/github/libretube/myApp.kt @@ -20,7 +20,7 @@ class myApp : Application() { // 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 importance = NotificationManager.IMPORTANCE_LOW val mChannel = NotificationChannel("background_mode", name, importance) mChannel.description = descriptionText