mirror of
https://github.com/libre-tube/LibreTube.git
synced 2024-12-15 06:40:30 +05:30
autoplay in background mode
This commit is contained in:
parent
37bba6ea95
commit
8f758566f1
@ -1,13 +1,16 @@
|
||||
package com.github.libretube
|
||||
|
||||
import android.app.NotificationManager
|
||||
import android.content.Context
|
||||
import android.support.v4.media.session.MediaSessionCompat
|
||||
import com.github.libretube.obj.Streams
|
||||
import com.github.libretube.util.DescriptionAdapter
|
||||
import com.github.libretube.util.PreferenceHelper
|
||||
import com.github.libretube.util.RetrofitInstance
|
||||
import com.google.android.exoplayer2.C
|
||||
import com.google.android.exoplayer2.ExoPlayer
|
||||
import com.google.android.exoplayer2.MediaItem
|
||||
import com.google.android.exoplayer2.Player
|
||||
import com.google.android.exoplayer2.audio.AudioAttributes
|
||||
import com.google.android.exoplayer2.ext.mediasession.MediaSessionConnector
|
||||
import com.google.android.exoplayer2.ui.PlayerNotificationManager
|
||||
@ -42,7 +45,7 @@ class BackgroundMode {
|
||||
/**
|
||||
* The [PlayerNotificationManager] to load the [mediaSession] content on it.
|
||||
*/
|
||||
private lateinit var playerNotification: PlayerNotificationManager
|
||||
private var playerNotification: PlayerNotificationManager? = null
|
||||
|
||||
/**
|
||||
* The [AudioAttributes] handle the audio focus of the [player]
|
||||
@ -63,9 +66,45 @@ class BackgroundMode {
|
||||
.setAudioAttributes(audioAttributes, true)
|
||||
.build()
|
||||
}
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
val autoplay = PreferenceHelper.getBoolean(c, "autoplay", false)
|
||||
if (state == Player.STATE_ENDED) {
|
||||
if (autoplay) playNextVideo(c)
|
||||
}
|
||||
}
|
||||
})
|
||||
setMediaItem(c)
|
||||
}
|
||||
|
||||
/**
|
||||
* 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)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes the [playerNotification] attached to the [player] and shows it.
|
||||
*/
|
||||
@ -82,10 +121,12 @@ class BackgroundMode {
|
||||
)
|
||||
)
|
||||
.build()
|
||||
playerNotification.apply {
|
||||
playerNotification?.apply {
|
||||
setPlayer(player)
|
||||
setUsePreviousAction(false)
|
||||
setUseNextAction(false)
|
||||
setUsePreviousAction(false)
|
||||
setUseStopAction(true)
|
||||
setColorized(true)
|
||||
setMediaSessionToken(mediaSession.sessionToken)
|
||||
}
|
||||
}
|
||||
@ -110,7 +151,11 @@ class BackgroundMode {
|
||||
/**
|
||||
* Gets the video data and prepares the [player].
|
||||
*/
|
||||
fun playOnBackgroundMode(c: Context, videoId: String, seekToPosition: Long) {
|
||||
fun playOnBackgroundMode(
|
||||
c: Context,
|
||||
videoId: String,
|
||||
seekToPosition: Long = 0
|
||||
) {
|
||||
runBlocking {
|
||||
val job = launch {
|
||||
response = RetrofitInstance.api.getStreams(videoId)
|
||||
|
@ -47,7 +47,7 @@ class VideoOptionsDialog(private val videoId: String, context: Context) : Dialog
|
||||
// This for example will be the "Background mode" option
|
||||
0 -> {
|
||||
BackgroundMode.getInstance()
|
||||
.playOnBackgroundMode(requireContext(), videoId, 0)
|
||||
.playOnBackgroundMode(requireContext(), videoId)
|
||||
}
|
||||
// Add Video to Playlist Dialog
|
||||
1 -> {
|
||||
|
@ -1006,8 +1006,8 @@ class PlayerFragment : Fragment() {
|
||||
|
||||
playerNotification.apply {
|
||||
setPlayer(exoPlayer)
|
||||
setUseNextAction(false)
|
||||
setUsePreviousAction(false)
|
||||
setUseStopAction(true)
|
||||
setMediaSessionToken(mediaSession.sessionToken)
|
||||
}
|
||||
}
|
||||
|
@ -83,8 +83,8 @@ class DescriptionAdapter(
|
||||
return try {
|
||||
val resizedBitmap = Bitmap.createScaledBitmap(
|
||||
bitmap,
|
||||
1080,
|
||||
1080,
|
||||
bitmap.width,
|
||||
bitmap.width,
|
||||
false
|
||||
)
|
||||
resizedBitmap
|
||||
|
Loading…
Reference in New Issue
Block a user