2022-07-01 20:24:20 +05:30
|
|
|
package com.github.libretube.util
|
2022-05-08 16:11:08 +05:30
|
|
|
|
2022-06-30 19:32:55 +05:30
|
|
|
import android.app.NotificationManager
|
2022-05-08 16:11:08 +05:30
|
|
|
import android.content.Context
|
2022-05-27 01:08:25 +05:30
|
|
|
import android.support.v4.media.session.MediaSessionCompat
|
2022-05-08 16:11:08 +05:30
|
|
|
import com.github.libretube.obj.Streams
|
2022-07-02 21:53:24 +05:30
|
|
|
import com.github.libretube.preferences.PreferenceHelper
|
2022-07-17 21:48:39 +05:30
|
|
|
import com.github.libretube.preferences.PreferenceKeys
|
2022-05-27 22:49:08 +05:30
|
|
|
import com.google.android.exoplayer2.C
|
2022-05-08 16:11:08 +05:30
|
|
|
import com.google.android.exoplayer2.ExoPlayer
|
|
|
|
import com.google.android.exoplayer2.MediaItem
|
2022-06-30 19:32:55 +05:30
|
|
|
import com.google.android.exoplayer2.Player
|
2022-05-27 22:49:08 +05:30
|
|
|
import com.google.android.exoplayer2.audio.AudioAttributes
|
2022-05-27 01:08:25 +05:30
|
|
|
import com.google.android.exoplayer2.ext.mediasession.MediaSessionConnector
|
2022-05-20 17:50:26 +05:30
|
|
|
import com.google.android.exoplayer2.ui.PlayerNotificationManager
|
2022-05-08 16:11:08 +05:30
|
|
|
import kotlinx.coroutines.launch
|
|
|
|
import kotlinx.coroutines.runBlocking
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads the selected video audio in background mode with a notification area.
|
|
|
|
*/
|
2022-05-20 01:43:42 +05:30
|
|
|
class BackgroundMode {
|
2022-05-08 16:11:08 +05:30
|
|
|
/**
|
|
|
|
* The response that gets when called the Api.
|
|
|
|
*/
|
|
|
|
private var response: Streams? = null
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The [ExoPlayer] player. Followed tutorial [here](https://developer.android.com/codelabs/exoplayer-intro)
|
|
|
|
*/
|
|
|
|
private var player: ExoPlayer? = null
|
|
|
|
private var playWhenReadyPlayer = true
|
|
|
|
|
2022-05-27 01:08:25 +05:30
|
|
|
/**
|
|
|
|
* 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
|
2022-05-20 17:50:26 +05:30
|
|
|
|
2022-05-08 16:11:08 +05:30
|
|
|
/**
|
2022-05-27 01:08:25 +05:30
|
|
|
* The [PlayerNotificationManager] to load the [mediaSession] content on it.
|
|
|
|
*/
|
2022-06-30 19:32:55 +05:30
|
|
|
private var playerNotification: PlayerNotificationManager? = null
|
2022-05-27 01:08:25 +05:30
|
|
|
|
2022-06-14 22:38:02 +05:30
|
|
|
/**
|
|
|
|
* The [AudioAttributes] handle the audio focus of the [player]
|
|
|
|
*/
|
|
|
|
private lateinit var audioAttributes: AudioAttributes
|
|
|
|
|
2022-05-27 01:08:25 +05:30
|
|
|
/**
|
|
|
|
* Initializes the [player] with the [MediaItem].
|
2022-05-08 16:11:08 +05:30
|
|
|
*/
|
2022-05-20 01:43:42 +05:30
|
|
|
private fun initializePlayer(c: Context) {
|
2022-06-14 22:38:02 +05:30
|
|
|
audioAttributes = AudioAttributes.Builder()
|
2022-05-27 22:49:08 +05:30
|
|
|
.setUsage(C.USAGE_MEDIA)
|
|
|
|
.setContentType(C.CONTENT_TYPE_MUSIC)
|
|
|
|
.build()
|
|
|
|
|
|
|
|
if (player == null) {
|
|
|
|
player = ExoPlayer.Builder(c)
|
|
|
|
.setAudioAttributes(audioAttributes, true)
|
|
|
|
.build()
|
|
|
|
}
|
2022-06-30 19:32:55 +05:30
|
|
|
|
|
|
|
/**
|
|
|
|
* Listens for changed playbackStates (e.g. pause, end)
|
|
|
|
* Plays the next video when the current one ended
|
|
|
|
*/
|
|
|
|
player!!.addListener(object : Player.Listener {
|
|
|
|
override fun onPlaybackStateChanged(@Player.State state: Int) {
|
2022-07-17 21:48:39 +05:30
|
|
|
val autoplay = PreferenceHelper.getBoolean(PreferenceKeys.AUTO_PLAY, false)
|
2022-06-30 19:32:55 +05:30
|
|
|
if (state == Player.STATE_ENDED) {
|
|
|
|
if (autoplay) playNextVideo(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2022-05-27 01:08:25 +05:30
|
|
|
setMediaItem(c)
|
2022-05-08 16:11:08 +05:30
|
|
|
}
|
|
|
|
|
2022-06-30 19:32:55 +05:30
|
|
|
/**
|
|
|
|
* Plays the first related video to the current (used when the playback of the current video ended)
|
|
|
|
*/
|
|
|
|
private fun playNextVideo(c: Context) {
|
|
|
|
if (response!!.relatedStreams!!.isNotEmpty()) {
|
|
|
|
val videoId = response!!
|
|
|
|
.relatedStreams!![0].url!!
|
|
|
|
.replace("/watch?v=", "")
|
|
|
|
|
|
|
|
// destroy old player and its notification
|
|
|
|
playerNotification = null
|
|
|
|
player = null
|
|
|
|
|
|
|
|
// kill old notification
|
|
|
|
val notificationManager = c.getSystemService(Context.NOTIFICATION_SERVICE)
|
|
|
|
as NotificationManager
|
|
|
|
notificationManager.cancel(1)
|
|
|
|
|
|
|
|
// play new video on background
|
|
|
|
playOnBackgroundMode(c, videoId)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-20 17:50:26 +05:30
|
|
|
/**
|
2022-05-27 01:08:25 +05:30
|
|
|
* Initializes the [playerNotification] attached to the [player] and shows it.
|
2022-05-20 17:50:26 +05:30
|
|
|
*/
|
2022-06-14 22:38:02 +05:30
|
|
|
private fun initializePlayerNotification(c: Context) {
|
2022-06-01 15:33:00 +05:30
|
|
|
playerNotification = PlayerNotificationManager
|
2022-06-14 15:51:18 +05:30
|
|
|
.Builder(c, 1, "background_mode")
|
|
|
|
// set the description of the notification
|
|
|
|
.setMediaDescriptionAdapter(
|
2022-06-14 22:38:02 +05:30
|
|
|
DescriptionAdapter(
|
|
|
|
response?.title!!,
|
|
|
|
response?.uploader!!,
|
2022-06-19 01:14:22 +05:30
|
|
|
response?.thumbnailUrl!!,
|
|
|
|
c
|
2022-06-14 22:38:02 +05:30
|
|
|
)
|
2022-06-14 15:51:18 +05:30
|
|
|
)
|
|
|
|
.build()
|
2022-06-30 19:32:55 +05:30
|
|
|
playerNotification?.apply {
|
2022-06-14 15:51:18 +05:30
|
|
|
setPlayer(player)
|
|
|
|
setUseNextAction(false)
|
2022-06-30 19:32:55 +05:30
|
|
|
setUsePreviousAction(false)
|
|
|
|
setUseStopAction(true)
|
|
|
|
setColorized(true)
|
2022-06-14 15:51:18 +05:30
|
|
|
setMediaSessionToken(mediaSession.sessionToken)
|
|
|
|
}
|
2022-05-20 17:50:26 +05:30
|
|
|
}
|
|
|
|
|
2022-05-20 01:43:42 +05:30
|
|
|
/**
|
2022-05-27 01:08:25 +05:30
|
|
|
* Sets the [MediaItem] with the [response] into the [player]. Also creates a [MediaSessionConnector]
|
|
|
|
* with the [mediaSession] and attach it to the [player].
|
2022-05-20 01:43:42 +05:30
|
|
|
*/
|
2022-05-27 01:08:25 +05:30
|
|
|
private fun setMediaItem(c: Context) {
|
2022-05-20 01:43:42 +05:30
|
|
|
response?.let {
|
2022-05-27 01:08:25 +05:30
|
|
|
val mediaItem = MediaItem.Builder().setUri(it.hls!!).build()
|
2022-05-20 01:43:42 +05:30
|
|
|
player?.setMediaItem(mediaItem)
|
|
|
|
}
|
2022-05-27 01:08:25 +05:30
|
|
|
|
|
|
|
mediaSession = MediaSessionCompat(c, this.javaClass.name)
|
|
|
|
mediaSession.isActive = true
|
|
|
|
|
|
|
|
mediaSessionConnector = MediaSessionConnector(mediaSession)
|
|
|
|
mediaSessionConnector.setPlayer(player)
|
2022-05-20 01:43:42 +05:30
|
|
|
}
|
|
|
|
|
2022-05-08 16:11:08 +05:30
|
|
|
/**
|
|
|
|
* Gets the video data and prepares the [player].
|
|
|
|
*/
|
2022-06-30 19:32:55 +05:30
|
|
|
fun playOnBackgroundMode(
|
|
|
|
c: Context,
|
|
|
|
videoId: String,
|
|
|
|
seekToPosition: Long = 0
|
|
|
|
) {
|
2022-05-08 16:11:08 +05:30
|
|
|
runBlocking {
|
|
|
|
val job = launch {
|
|
|
|
response = RetrofitInstance.api.getStreams(videoId)
|
|
|
|
}
|
|
|
|
// Wait until the job is done, to load correctly later in the player
|
|
|
|
job.join()
|
|
|
|
|
2022-05-20 01:43:42 +05:30
|
|
|
initializePlayer(c)
|
2022-06-14 22:38:02 +05:30
|
|
|
initializePlayerNotification(c)
|
2022-05-08 16:11:08 +05:30
|
|
|
|
|
|
|
player?.apply {
|
|
|
|
playWhenReady = playWhenReadyPlayer
|
|
|
|
prepare()
|
|
|
|
}
|
2022-05-27 22:49:08 +05:30
|
|
|
|
|
|
|
if (!seekToPosition.equals(0)) player?.seekTo(seekToPosition)
|
2022-05-08 16:11:08 +05:30
|
|
|
}
|
|
|
|
}
|
2022-05-20 01:43:42 +05:30
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a singleton of this class, to not create a new [player] every time.
|
|
|
|
*/
|
|
|
|
companion object {
|
|
|
|
private var INSTANCE: BackgroundMode? = null
|
|
|
|
|
|
|
|
fun getInstance(): BackgroundMode {
|
|
|
|
if (INSTANCE == null) INSTANCE = BackgroundMode()
|
|
|
|
return INSTANCE!!
|
|
|
|
}
|
|
|
|
}
|
2022-05-08 16:11:08 +05:30
|
|
|
}
|