mirror of
https://github.com/libre-tube/LibreTube.git
synced 2025-04-28 07:50:31 +05:30
use for player fragment too
This commit is contained in:
parent
3399e4a8a8
commit
955d6de690
@ -1,7 +1,6 @@
|
|||||||
package com.github.libretube.fragments
|
package com.github.libretube.fragments
|
||||||
|
|
||||||
import android.app.ActivityManager
|
import android.app.ActivityManager
|
||||||
import android.app.NotificationManager
|
|
||||||
import android.app.PictureInPictureParams
|
import android.app.PictureInPictureParams
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
@ -16,7 +15,6 @@ import android.os.Bundle
|
|||||||
import android.os.Handler
|
import android.os.Handler
|
||||||
import android.os.Looper
|
import android.os.Looper
|
||||||
import android.os.PowerManager
|
import android.os.PowerManager
|
||||||
import android.support.v4.media.session.MediaSessionCompat
|
|
||||||
import android.text.Html
|
import android.text.Html
|
||||||
import android.text.format.DateUtils
|
import android.text.format.DateUtils
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
@ -35,9 +33,7 @@ import androidx.lifecycle.lifecycleScope
|
|||||||
import androidx.recyclerview.widget.GridLayoutManager
|
import androidx.recyclerview.widget.GridLayoutManager
|
||||||
import androidx.recyclerview.widget.LinearLayoutManager
|
import androidx.recyclerview.widget.LinearLayoutManager
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper
|
import com.fasterxml.jackson.databind.ObjectMapper
|
||||||
import com.github.libretube.BACKGROUND_CHANNEL_ID
|
|
||||||
import com.github.libretube.Globals
|
import com.github.libretube.Globals
|
||||||
import com.github.libretube.PLAYER_NOTIFICATION_ID
|
|
||||||
import com.github.libretube.R
|
import com.github.libretube.R
|
||||||
import com.github.libretube.activities.MainActivity
|
import com.github.libretube.activities.MainActivity
|
||||||
import com.github.libretube.adapters.ChaptersAdapter
|
import com.github.libretube.adapters.ChaptersAdapter
|
||||||
@ -60,7 +56,7 @@ import com.github.libretube.services.BackgroundMode
|
|||||||
import com.github.libretube.util.BackgroundHelper
|
import com.github.libretube.util.BackgroundHelper
|
||||||
import com.github.libretube.util.ConnectionHelper
|
import com.github.libretube.util.ConnectionHelper
|
||||||
import com.github.libretube.util.CronetHelper
|
import com.github.libretube.util.CronetHelper
|
||||||
import com.github.libretube.util.DescriptionAdapter
|
import com.github.libretube.util.NowPlayingNotification
|
||||||
import com.github.libretube.util.OnDoubleTapEventListener
|
import com.github.libretube.util.OnDoubleTapEventListener
|
||||||
import com.github.libretube.util.PlayerHelper
|
import com.github.libretube.util.PlayerHelper
|
||||||
import com.github.libretube.util.RetrofitInstance
|
import com.github.libretube.util.RetrofitInstance
|
||||||
@ -77,7 +73,6 @@ import com.google.android.exoplayer2.MediaItem.fromUri
|
|||||||
import com.google.android.exoplayer2.Player
|
import com.google.android.exoplayer2.Player
|
||||||
import com.google.android.exoplayer2.audio.AudioAttributes
|
import com.google.android.exoplayer2.audio.AudioAttributes
|
||||||
import com.google.android.exoplayer2.ext.cronet.CronetDataSource
|
import com.google.android.exoplayer2.ext.cronet.CronetDataSource
|
||||||
import com.google.android.exoplayer2.ext.mediasession.MediaSessionConnector
|
|
||||||
import com.google.android.exoplayer2.source.DefaultMediaSourceFactory
|
import com.google.android.exoplayer2.source.DefaultMediaSourceFactory
|
||||||
import com.google.android.exoplayer2.source.MediaSource
|
import com.google.android.exoplayer2.source.MediaSource
|
||||||
import com.google.android.exoplayer2.source.MergingMediaSource
|
import com.google.android.exoplayer2.source.MergingMediaSource
|
||||||
@ -85,7 +80,6 @@ import com.google.android.exoplayer2.source.ProgressiveMediaSource
|
|||||||
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector
|
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector
|
||||||
import com.google.android.exoplayer2.ui.AspectRatioFrameLayout
|
import com.google.android.exoplayer2.ui.AspectRatioFrameLayout
|
||||||
import com.google.android.exoplayer2.ui.CaptionStyleCompat
|
import com.google.android.exoplayer2.ui.CaptionStyleCompat
|
||||||
import com.google.android.exoplayer2.ui.PlayerNotificationManager
|
|
||||||
import com.google.android.exoplayer2.ui.StyledPlayerView
|
import com.google.android.exoplayer2.ui.StyledPlayerView
|
||||||
import com.google.android.exoplayer2.ui.TimeBar
|
import com.google.android.exoplayer2.ui.TimeBar
|
||||||
import com.google.android.exoplayer2.upstream.DataSource
|
import com.google.android.exoplayer2.upstream.DataSource
|
||||||
@ -181,9 +175,7 @@ class PlayerFragment : Fragment() {
|
|||||||
/**
|
/**
|
||||||
* for the player notification
|
* for the player notification
|
||||||
*/
|
*/
|
||||||
private lateinit var mediaSession: MediaSessionCompat
|
private lateinit var nowPlayingNotification: NowPlayingNotification
|
||||||
private lateinit var mediaSessionConnector: MediaSessionConnector
|
|
||||||
private lateinit var playerNotification: PlayerNotificationManager
|
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
@ -676,15 +668,7 @@ class PlayerFragment : Fragment() {
|
|||||||
super.onDestroy()
|
super.onDestroy()
|
||||||
try {
|
try {
|
||||||
saveWatchPosition()
|
saveWatchPosition()
|
||||||
mediaSession.isActive = false
|
nowPlayingNotification.destroy()
|
||||||
mediaSession.release()
|
|
||||||
mediaSessionConnector.setPlayer(null)
|
|
||||||
playerNotification.setPlayer(null)
|
|
||||||
val notificationManager = context?.getSystemService(
|
|
||||||
Context.NOTIFICATION_SERVICE
|
|
||||||
) as NotificationManager
|
|
||||||
notificationManager.cancel(1)
|
|
||||||
exoPlayer.release()
|
|
||||||
activity?.requestedOrientation =
|
activity?.requestedOrientation =
|
||||||
if ((activity as MainActivity).autoRotationEnabled) ActivityInfo.SCREEN_ORIENTATION_USER
|
if ((activity as MainActivity).autoRotationEnabled) ActivityInfo.SCREEN_ORIENTATION_USER
|
||||||
else ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT
|
else ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT
|
||||||
@ -752,7 +736,7 @@ class PlayerFragment : Fragment() {
|
|||||||
exoPlayer.prepare()
|
exoPlayer.prepare()
|
||||||
exoPlayer.play()
|
exoPlayer.play()
|
||||||
exoPlayerView.useController = true
|
exoPlayerView.useController = true
|
||||||
initializePlayerNotification(requireContext())
|
initializePlayerNotification()
|
||||||
if (sponsorBlockEnabled) fetchSponsorBlockSegments()
|
if (sponsorBlockEnabled) fetchSponsorBlockSegments()
|
||||||
// show comments if related streams disabled
|
// show comments if related streams disabled
|
||||||
if (!relatedStreamsEnabled) toggleComments()
|
if (!relatedStreamsEnabled) toggleComments()
|
||||||
@ -1502,33 +1486,14 @@ class PlayerFragment : Fragment() {
|
|||||||
exoPlayer.setAudioAttributes(audioAttributes, true)
|
exoPlayer.setAudioAttributes(audioAttributes, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun initializePlayerNotification(c: Context) {
|
/**
|
||||||
mediaSession = MediaSessionCompat(c, this.javaClass.name)
|
* show the [NowPlayingNotification] for the current video
|
||||||
mediaSession.apply {
|
*/
|
||||||
isActive = true
|
private fun initializePlayerNotification() {
|
||||||
}
|
if (!this::nowPlayingNotification.isInitialized) {
|
||||||
|
nowPlayingNotification = NowPlayingNotification(requireContext(), exoPlayer)
|
||||||
mediaSessionConnector = MediaSessionConnector(mediaSession)
|
|
||||||
mediaSessionConnector.setPlayer(exoPlayer)
|
|
||||||
|
|
||||||
playerNotification = PlayerNotificationManager
|
|
||||||
.Builder(c, PLAYER_NOTIFICATION_ID, BACKGROUND_CHANNEL_ID)
|
|
||||||
.setMediaDescriptionAdapter(
|
|
||||||
DescriptionAdapter(
|
|
||||||
streams.title!!,
|
|
||||||
streams.uploader!!,
|
|
||||||
streams.thumbnailUrl!!,
|
|
||||||
requireContext()
|
|
||||||
)
|
|
||||||
)
|
|
||||||
.build()
|
|
||||||
|
|
||||||
playerNotification.apply {
|
|
||||||
setPlayer(exoPlayer)
|
|
||||||
setUsePreviousAction(false)
|
|
||||||
setUseStopAction(true)
|
|
||||||
setMediaSessionToken(mediaSession.sessionToken)
|
|
||||||
}
|
}
|
||||||
|
nowPlayingNotification.updatePlayerNotification(streams)
|
||||||
}
|
}
|
||||||
|
|
||||||
// lock the player
|
// lock the player
|
||||||
|
@ -1,95 +0,0 @@
|
|||||||
package com.github.libretube.util
|
|
||||||
|
|
||||||
import android.app.PendingIntent
|
|
||||||
import android.content.Context
|
|
||||||
import android.content.Intent
|
|
||||||
import android.graphics.Bitmap
|
|
||||||
import android.graphics.BitmapFactory
|
|
||||||
import com.github.libretube.activities.MainActivity
|
|
||||||
import com.google.android.exoplayer2.Player
|
|
||||||
import com.google.android.exoplayer2.ui.PlayerNotificationManager
|
|
||||||
import java.net.URL
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The [DescriptionAdapter] is used to show title, uploaderName and thumbnail of the video in the notification
|
|
||||||
* Basic example [here](https://github.com/AnthonyMarkD/AudioPlayerSampleTest)
|
|
||||||
*/
|
|
||||||
class DescriptionAdapter(
|
|
||||||
private val title: String,
|
|
||||||
private val channelName: String,
|
|
||||||
private val thumbnailUrl: String,
|
|
||||||
private val context: Context
|
|
||||||
) :
|
|
||||||
PlayerNotificationManager.MediaDescriptionAdapter {
|
|
||||||
/**
|
|
||||||
* sets the title of the notification
|
|
||||||
*/
|
|
||||||
override fun getCurrentContentTitle(player: Player): CharSequence {
|
|
||||||
// return controller.metadata.description.title.toString()
|
|
||||||
return 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(context, MainActivity::class.java)
|
|
||||||
return PendingIntent.getActivity(context, 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 channelName
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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(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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,5 +1,6 @@
|
|||||||
package com.github.libretube.util
|
package com.github.libretube.util
|
||||||
|
|
||||||
|
import android.app.NotificationManager
|
||||||
import android.app.PendingIntent
|
import android.app.PendingIntent
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
@ -155,4 +156,16 @@ class NowPlayingNotification(
|
|||||||
setMediaSessionToken(mediaSession.sessionToken)
|
setMediaSessionToken(mediaSession.sessionToken)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun destroy() {
|
||||||
|
mediaSession.isActive = false
|
||||||
|
mediaSession.release()
|
||||||
|
mediaSessionConnector.setPlayer(null)
|
||||||
|
playerNotification?.setPlayer(null)
|
||||||
|
val notificationManager = context.getSystemService(
|
||||||
|
Context.NOTIFICATION_SERVICE
|
||||||
|
) as NotificationManager
|
||||||
|
notificationManager.cancel(PLAYER_NOTIFICATION_ID)
|
||||||
|
player.release()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user