2022-07-21 16:40:27 +05:30
|
|
|
package com.github.libretube.services
|
2022-05-08 16:11:08 +05:30
|
|
|
|
2022-07-23 19:11:57 +05:30
|
|
|
import android.app.Notification
|
|
|
|
import android.app.NotificationChannel
|
2022-06-30 19:32:55 +05:30
|
|
|
import android.app.NotificationManager
|
2022-08-07 21:40:13 +05:30
|
|
|
import android.app.PendingIntent
|
2022-07-21 16:40:27 +05:30
|
|
|
import android.app.Service
|
|
|
|
import android.content.Intent
|
2022-08-07 21:40:13 +05:30
|
|
|
import android.graphics.Bitmap
|
|
|
|
import android.graphics.BitmapFactory
|
2022-07-23 19:11:57 +05:30
|
|
|
import android.os.Build
|
2022-08-02 15:04:42 +05:30
|
|
|
import android.os.Handler
|
2022-07-21 16:40:27 +05:30
|
|
|
import android.os.IBinder
|
2022-08-02 15:04:42 +05:30
|
|
|
import android.os.Looper
|
2022-05-27 01:08:25 +05:30
|
|
|
import android.support.v4.media.session.MediaSessionCompat
|
2022-08-02 15:04:42 +05:30
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper
|
2022-07-30 14:51:18 +05:30
|
|
|
import com.github.libretube.BACKGROUND_CHANNEL_ID
|
|
|
|
import com.github.libretube.PLAYER_NOTIFICATION_ID
|
2022-07-23 19:11:57 +05:30
|
|
|
import com.github.libretube.R
|
2022-08-07 21:40:13 +05:30
|
|
|
import com.github.libretube.activities.MainActivity
|
2022-08-02 15:04:42 +05:30
|
|
|
import com.github.libretube.obj.Segment
|
|
|
|
import com.github.libretube.obj.Segments
|
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-07-21 16:40:27 +05:30
|
|
|
import com.github.libretube.util.DescriptionAdapter
|
2022-08-02 15:04:42 +05:30
|
|
|
import com.github.libretube.util.PlayerHelper
|
2022-07-21 16:40:27 +05:30
|
|
|
import com.github.libretube.util.RetrofitInstance
|
2022-07-29 12:30:13 +05:30
|
|
|
import com.github.libretube.util.toID
|
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-08-02 15:04:42 +05:30
|
|
|
import kotlinx.coroutines.CoroutineScope
|
|
|
|
import kotlinx.coroutines.Dispatchers
|
2022-05-08 16:11:08 +05:30
|
|
|
import kotlinx.coroutines.launch
|
|
|
|
import kotlinx.coroutines.runBlocking
|
2022-08-07 21:40:13 +05:30
|
|
|
import java.net.URL
|
2022-05-08 16:11:08 +05:30
|
|
|
|
|
|
|
/**
|
2022-07-21 16:40:27 +05:30
|
|
|
* Loads the selected videos audio in background mode with a notification area.
|
2022-05-08 16:11:08 +05:30
|
|
|
*/
|
2022-07-21 16:40:27 +05:30
|
|
|
class BackgroundMode : Service() {
|
2022-08-02 15:04:42 +05:30
|
|
|
/**
|
|
|
|
* VideoId of the video
|
|
|
|
*/
|
|
|
|
private lateinit var videoId: String
|
|
|
|
|
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-08-02 15:04:42 +05:30
|
|
|
/**
|
|
|
|
* SponsorBlock Segment data
|
|
|
|
*/
|
|
|
|
private var segmentData: Segments? = null
|
|
|
|
|
2022-07-23 19:11:57 +05:30
|
|
|
override fun onCreate() {
|
|
|
|
super.onCreate()
|
2022-08-02 15:04:42 +05:30
|
|
|
/**
|
|
|
|
* setting the required notification for running as a foreground service
|
|
|
|
*/
|
2022-07-23 19:11:57 +05:30
|
|
|
if (Build.VERSION.SDK_INT >= 26) {
|
2022-08-01 12:25:38 +05:30
|
|
|
val channelId = BACKGROUND_CHANNEL_ID
|
2022-07-23 19:11:57 +05:30
|
|
|
val channel = NotificationChannel(
|
|
|
|
channelId,
|
2022-08-01 12:25:38 +05:30
|
|
|
"Background Service",
|
2022-07-23 19:11:57 +05:30
|
|
|
NotificationManager.IMPORTANCE_DEFAULT
|
|
|
|
)
|
|
|
|
val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
|
|
|
|
notificationManager.createNotificationChannel(channel)
|
|
|
|
val notification: Notification = Notification.Builder(this, channelId)
|
|
|
|
.setContentTitle(getString(R.string.app_name))
|
|
|
|
.setContentText(getString(R.string.playingOnBackground)).build()
|
2022-08-01 12:25:38 +05:30
|
|
|
startForeground(PLAYER_NOTIFICATION_ID, notification)
|
2022-07-23 19:11:57 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-27 01:08:25 +05:30
|
|
|
/**
|
|
|
|
* Initializes the [player] with the [MediaItem].
|
2022-05-08 16:11:08 +05:30
|
|
|
*/
|
2022-07-21 16:40:27 +05:30
|
|
|
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
2022-07-23 19:11:57 +05:30
|
|
|
// get the intent arguments
|
2022-08-02 15:04:42 +05:30
|
|
|
videoId = intent?.getStringExtra("videoId")!!
|
2022-07-23 19:11:57 +05:30
|
|
|
val position = intent.getLongExtra("position", 0L)
|
|
|
|
|
|
|
|
// play the audio in the background
|
|
|
|
playAudio(videoId, position)
|
2022-07-21 16:40:27 +05:30
|
|
|
return super.onStartCommand(intent, flags, startId)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the video data and prepares the [player].
|
|
|
|
*/
|
2022-07-23 19:11:57 +05:30
|
|
|
private fun playAudio(
|
2022-07-21 16:40:27 +05:30
|
|
|
videoId: String,
|
|
|
|
seekToPosition: Long = 0
|
|
|
|
) {
|
|
|
|
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-07-23 19:11:57 +05:30
|
|
|
initializePlayer()
|
|
|
|
initializePlayerNotification()
|
2022-07-21 16:40:27 +05:30
|
|
|
|
|
|
|
player?.apply {
|
|
|
|
playWhenReady = playWhenReadyPlayer
|
|
|
|
prepare()
|
|
|
|
}
|
|
|
|
|
2022-07-23 19:11:57 +05:30
|
|
|
// seek to the previous position if available
|
2022-07-21 16:40:27 +05:30
|
|
|
if (seekToPosition != 0L) player?.seekTo(seekToPosition)
|
2022-08-02 15:04:42 +05:30
|
|
|
|
|
|
|
fetchSponsorBlockSegments()
|
2022-07-21 16:40:27 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* create the player
|
|
|
|
*/
|
2022-07-23 19:11:57 +05:30
|
|
|
private fun initializePlayer() {
|
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) {
|
2022-07-23 19:11:57 +05:30
|
|
|
player = ExoPlayer.Builder(this)
|
2022-05-27 22:49:08 +05:30
|
|
|
.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-27 14:59:16 +05:30
|
|
|
when (state) {
|
|
|
|
Player.STATE_ENDED -> {
|
|
|
|
val autoplay = PreferenceHelper.getBoolean(PreferenceKeys.AUTO_PLAY, true)
|
|
|
|
if (autoplay) playNextVideo()
|
|
|
|
}
|
|
|
|
Player.STATE_IDLE -> {
|
|
|
|
// called when the user pressed stop in the notification
|
|
|
|
// stop the service from being in the foreground and remove the notification
|
|
|
|
stopForeground(true)
|
|
|
|
// destroy the service
|
|
|
|
stopSelf()
|
|
|
|
}
|
2022-06-30 19:32:55 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2022-07-23 19:11:57 +05:30
|
|
|
setMediaItem()
|
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)
|
|
|
|
*/
|
2022-07-23 19:11:57 +05:30
|
|
|
private fun playNextVideo() {
|
2022-06-30 19:32:55 +05:30
|
|
|
if (response!!.relatedStreams!!.isNotEmpty()) {
|
|
|
|
val videoId = response!!
|
2022-07-29 12:30:13 +05:30
|
|
|
.relatedStreams!![0].url.toID()
|
2022-06-30 19:32:55 +05:30
|
|
|
|
|
|
|
// play new video on background
|
2022-08-02 15:04:42 +05:30
|
|
|
this.videoId = videoId
|
|
|
|
this.segmentData = null
|
2022-07-23 19:11:57 +05:30
|
|
|
playAudio(videoId)
|
2022-06-30 19:32:55 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-07 21:40:13 +05:30
|
|
|
/**
|
|
|
|
* The [DescriptionAdapter] is used to show title, uploaderName and thumbnail of the video in the notification
|
|
|
|
* Basic example [here](https://github.com/AnthonyMarkD/AudioPlayerSampleTest)
|
|
|
|
*/
|
|
|
|
inner class DescriptionAdapter() :
|
|
|
|
PlayerNotificationManager.MediaDescriptionAdapter {
|
|
|
|
/**
|
|
|
|
* sets the title of the notification
|
|
|
|
*/
|
|
|
|
override fun getCurrentContentTitle(player: Player): CharSequence {
|
|
|
|
// return controller.metadata.description.title.toString()
|
|
|
|
return response?.title!!
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* overrides the action when clicking the notification
|
|
|
|
*/
|
|
|
|
override fun createCurrentContentIntent(player: Player): PendingIntent? {
|
|
|
|
// return controller.sessionActivity
|
|
|
|
/**
|
|
|
|
* starts a new MainActivity Intent when the player notification is clicked
|
|
|
|
* it doesn't start a completely new MainActivity because the MainActivity's launchMode
|
|
|
|
* is set to "singleTop" in the AndroidManifest (important!!!)
|
|
|
|
* that's the only way to launch back into the previous activity (e.g. the player view
|
|
|
|
*/
|
|
|
|
val intent = Intent(this@BackgroundMode, MainActivity::class.java)
|
|
|
|
return PendingIntent.getActivity(this@BackgroundMode, 0, intent, PendingIntent.FLAG_IMMUTABLE)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* the description of the notification (below the title)
|
|
|
|
*/
|
|
|
|
override fun getCurrentContentText(player: Player): CharSequence? {
|
|
|
|
// return controller.metadata.description.subtitle.toString()
|
|
|
|
return response?.uploader
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* return the icon/thumbnail of the video
|
|
|
|
*/
|
|
|
|
override fun getCurrentLargeIcon(
|
|
|
|
player: Player,
|
|
|
|
callback: PlayerNotificationManager.BitmapCallback
|
|
|
|
): Bitmap? {
|
|
|
|
lateinit var bitmap: Bitmap
|
|
|
|
|
|
|
|
/**
|
|
|
|
* running on a new thread to prevent a NetworkMainThreadException
|
|
|
|
*/
|
|
|
|
val thread = Thread {
|
|
|
|
try {
|
|
|
|
/**
|
|
|
|
* try to GET the thumbnail from the URL
|
|
|
|
*/
|
|
|
|
val inputStream = URL(response?.thumbnailUrl).openStream()
|
|
|
|
bitmap = BitmapFactory.decodeStream(inputStream)
|
|
|
|
} catch (ex: java.lang.Exception) {
|
|
|
|
ex.printStackTrace()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
thread.start()
|
|
|
|
thread.join()
|
|
|
|
/**
|
|
|
|
* returns the scaled bitmap if it got fetched successfully
|
|
|
|
*/
|
|
|
|
return try {
|
|
|
|
val resizedBitmap = Bitmap.createScaledBitmap(
|
|
|
|
bitmap,
|
|
|
|
bitmap.width,
|
|
|
|
bitmap.width,
|
|
|
|
false
|
|
|
|
)
|
|
|
|
resizedBitmap
|
|
|
|
} catch (e: Exception) {
|
|
|
|
null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-07-23 19:11:57 +05:30
|
|
|
private fun initializePlayerNotification() {
|
2022-06-01 15:33:00 +05:30
|
|
|
playerNotification = PlayerNotificationManager
|
2022-07-30 14:51:18 +05:30
|
|
|
.Builder(this, PLAYER_NOTIFICATION_ID, BACKGROUND_CHANNEL_ID)
|
2022-06-14 15:51:18 +05:30
|
|
|
// set the description of the notification
|
|
|
|
.setMediaDescriptionAdapter(
|
2022-08-07 21:40:13 +05:30
|
|
|
DescriptionAdapter()
|
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-07-23 19:11:57 +05:30
|
|
|
private fun setMediaItem() {
|
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
|
|
|
|
2022-07-23 19:11:57 +05:30
|
|
|
mediaSession = MediaSessionCompat(this, this.javaClass.name)
|
2022-05-27 01:08:25 +05:30
|
|
|
mediaSession.isActive = true
|
|
|
|
|
|
|
|
mediaSessionConnector = MediaSessionConnector(mediaSession)
|
|
|
|
mediaSessionConnector.setPlayer(player)
|
2022-05-20 01:43:42 +05:30
|
|
|
}
|
|
|
|
|
2022-08-02 15:04:42 +05:30
|
|
|
/**
|
|
|
|
* fetch the segments for SponsorBlock
|
|
|
|
*/
|
|
|
|
private fun fetchSponsorBlockSegments() {
|
|
|
|
CoroutineScope(Dispatchers.IO).launch {
|
|
|
|
kotlin.runCatching {
|
|
|
|
val categories = PlayerHelper.getSponsorBlockCategories()
|
|
|
|
if (categories.size > 0) {
|
|
|
|
segmentData =
|
|
|
|
RetrofitInstance.api.getSegments(
|
|
|
|
videoId,
|
|
|
|
ObjectMapper().writeValueAsString(categories)
|
|
|
|
)
|
|
|
|
checkForSegments()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* check for SponsorBlock segments
|
|
|
|
*/
|
|
|
|
private fun checkForSegments() {
|
|
|
|
Handler(Looper.getMainLooper()).postDelayed(this::checkForSegments, 100)
|
|
|
|
|
|
|
|
if (segmentData == null || segmentData!!.segments.isEmpty()) return
|
|
|
|
|
|
|
|
segmentData!!.segments.forEach { segment: Segment ->
|
|
|
|
val segmentStart = (segment.segment!![0] * 1000f).toLong()
|
|
|
|
val segmentEnd = (segment.segment[1] * 1000f).toLong()
|
|
|
|
val currentPosition = player?.currentPosition
|
|
|
|
if (currentPosition in segmentStart until segmentEnd) {
|
|
|
|
player?.seekTo(segmentEnd)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-21 16:40:27 +05:30
|
|
|
override fun onBind(p0: Intent?): IBinder? {
|
|
|
|
TODO("Not yet implemented")
|
2022-05-20 01:43:42 +05:30
|
|
|
}
|
2022-05-08 16:11:08 +05:30
|
|
|
}
|