mirror of
https://github.com/libre-tube/LibreTube.git
synced 2024-12-13 22:00:30 +05:30
use proper way
This commit is contained in:
parent
9c3436751c
commit
3399e4a8a8
@ -9,7 +9,6 @@ import android.os.Build
|
||||
import android.os.Handler
|
||||
import android.os.IBinder
|
||||
import android.os.Looper
|
||||
import android.support.v4.media.session.MediaSessionCompat
|
||||
import com.fasterxml.jackson.databind.ObjectMapper
|
||||
import com.github.libretube.BACKGROUND_CHANNEL_ID
|
||||
import com.github.libretube.PLAYER_NOTIFICATION_ID
|
||||
@ -54,16 +53,6 @@ class BackgroundMode : Service() {
|
||||
private var player: ExoPlayer? = null
|
||||
private var playWhenReadyPlayer = true
|
||||
|
||||
/**
|
||||
* 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
|
||||
|
||||
/**
|
||||
* The [AudioAttributes] handle the audio focus of the [player]
|
||||
*/
|
||||
@ -104,12 +93,17 @@ class BackgroundMode : Service() {
|
||||
* Initializes the [player] with the [MediaItem].
|
||||
*/
|
||||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
||||
// get the intent arguments
|
||||
videoId = intent?.getStringExtra("videoId")!!
|
||||
val position = intent.getLongExtra("position", 0L)
|
||||
try {
|
||||
// get the intent arguments
|
||||
videoId = intent?.getStringExtra("videoId")!!
|
||||
val position = intent.getLongExtra("position", 0L)
|
||||
|
||||
// play the audio in the background
|
||||
playAudio(videoId, position)
|
||||
// play the audio in the background
|
||||
playAudio(videoId, position)
|
||||
} catch (e: Exception) {
|
||||
stopForeground(true)
|
||||
stopSelf()
|
||||
}
|
||||
return super.onStartCommand(intent, flags, startId)
|
||||
}
|
||||
|
||||
@ -131,8 +125,10 @@ class BackgroundMode : Service() {
|
||||
setMediaItem()
|
||||
|
||||
// create the notification
|
||||
nowPlayingNotification = NowPlayingNotification(this@BackgroundMode, player!!)
|
||||
nowPlayingNotification.initializePlayerNotification(mediaSession, response!!)
|
||||
if (!this@BackgroundMode::nowPlayingNotification.isInitialized) {
|
||||
nowPlayingNotification = NowPlayingNotification(this@BackgroundMode, player!!)
|
||||
}
|
||||
nowPlayingNotification.updatePlayerNotification(response!!)
|
||||
|
||||
player?.apply {
|
||||
playWhenReady = playWhenReadyPlayer
|
||||
@ -208,12 +204,6 @@ class BackgroundMode : Service() {
|
||||
val mediaItem = MediaItem.Builder().setUri(it.hls!!).build()
|
||||
player?.setMediaItem(mediaItem)
|
||||
}
|
||||
|
||||
mediaSession = MediaSessionCompat(this, this.javaClass.name)
|
||||
mediaSession.isActive = true
|
||||
|
||||
mediaSessionConnector = MediaSessionConnector(mediaSession)
|
||||
mediaSessionConnector.setPlayer(player)
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -12,6 +12,7 @@ import com.github.libretube.activities.MainActivity
|
||||
import com.github.libretube.obj.Streams
|
||||
import com.google.android.exoplayer2.ExoPlayer
|
||||
import com.google.android.exoplayer2.Player
|
||||
import com.google.android.exoplayer2.ext.mediasession.MediaSessionConnector
|
||||
import com.google.android.exoplayer2.ui.PlayerNotificationManager
|
||||
import java.net.URL
|
||||
|
||||
@ -21,15 +22,21 @@ class NowPlayingNotification(
|
||||
) {
|
||||
private var streams: Streams? = null
|
||||
|
||||
/**
|
||||
* The [MediaSessionCompat] for the [streams].
|
||||
*/
|
||||
private lateinit var mediaSession: MediaSessionCompat
|
||||
|
||||
/**
|
||||
* 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 var playerNotification: PlayerNotificationManager? = null
|
||||
|
||||
private fun setStreams(streams: Streams) {
|
||||
this.streams = streams
|
||||
}
|
||||
|
||||
/**
|
||||
* The [DescriptionAdapter] is used to show title, uploaderName and thumbnail of the video in the notification
|
||||
* Basic example [here](https://github.com/AnthonyMarkD/AudioPlayerSampleTest)
|
||||
@ -109,14 +116,29 @@ class NowPlayingNotification(
|
||||
}
|
||||
}
|
||||
|
||||
private fun createMediaSession() {
|
||||
if (this::mediaSession.isInitialized) return
|
||||
mediaSession = MediaSessionCompat(context, this.javaClass.name)
|
||||
mediaSession.isActive = true
|
||||
|
||||
mediaSessionConnector = MediaSessionConnector(mediaSession)
|
||||
mediaSessionConnector.setPlayer(player)
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes the [playerNotification] attached to the [player] and shows it.
|
||||
*/
|
||||
fun initializePlayerNotification(
|
||||
mediaSession: MediaSessionCompat,
|
||||
fun updatePlayerNotification(
|
||||
streams: Streams
|
||||
) {
|
||||
this.streams = streams
|
||||
if (playerNotification == null) {
|
||||
createMediaSession()
|
||||
createNotification()
|
||||
}
|
||||
}
|
||||
|
||||
private fun createNotification() {
|
||||
playerNotification = PlayerNotificationManager
|
||||
.Builder(context, PLAYER_NOTIFICATION_ID, BACKGROUND_CHANNEL_ID)
|
||||
// set the description of the notification
|
||||
|
Loading…
Reference in New Issue
Block a user