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