Merge pull request #1914 from Bnyro/master

Fix crash on destroy of the player notification
This commit is contained in:
Bnyro 2022-11-19 15:19:16 +01:00 committed by GitHub
commit 19dd4a7a56
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 19 deletions

View File

@ -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.util.Log
import android.widget.Toast import android.widget.Toast
import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.databind.ObjectMapper
import com.github.libretube.R import com.github.libretube.R
@ -21,7 +20,6 @@ import com.github.libretube.constants.BACKGROUND_CHANNEL_ID
import com.github.libretube.constants.IntentData import com.github.libretube.constants.IntentData
import com.github.libretube.constants.PLAYER_NOTIFICATION_ID import com.github.libretube.constants.PLAYER_NOTIFICATION_ID
import com.github.libretube.constants.PreferenceKeys import com.github.libretube.constants.PreferenceKeys
import com.github.libretube.db.DatabaseHolder
import com.github.libretube.db.DatabaseHolder.Companion.Database import com.github.libretube.db.DatabaseHolder.Companion.Database
import com.github.libretube.db.obj.WatchPosition import com.github.libretube.db.obj.WatchPosition
import com.github.libretube.extensions.awaitQuery import com.github.libretube.extensions.awaitQuery
@ -117,7 +115,7 @@ class BackgroundMode : Service() {
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
try { try {
// clear the playing queue // clear the playing queue
PlayingQueue.clear() PlayingQueue.resetToDefaults()
// get the intent arguments // get the intent arguments
videoId = intent?.getStringExtra(IntentData.videoId)!! videoId = intent?.getStringExtra(IntentData.videoId)!!
@ -201,9 +199,8 @@ class BackgroundMode : Service() {
} else if (PlayerHelper.watchPositionsEnabled) { } else if (PlayerHelper.watchPositionsEnabled) {
try { try {
val watchPosition = awaitQuery { val watchPosition = awaitQuery {
DatabaseHolder.Database.watchPositionDao().findById(videoId) Database.watchPositionDao().findById(videoId)
} }
Log.e("position", watchPosition.toString())
streams?.duration?.let { streams?.duration?.let {
if (watchPosition != null && watchPosition.position < it * 1000 * 0.9) { if (watchPosition != null && watchPosition.position < it * 1000 * 0.9) {
player?.seekTo(watchPosition.position) player?.seekTo(watchPosition.position)
@ -355,7 +352,7 @@ class BackgroundMode : Service() {
*/ */
override fun onDestroy() { override fun onDestroy() {
// clear the playing queue // clear the playing queue
PlayingQueue.clear() PlayingQueue.resetToDefaults()
if (this::nowPlayingNotification.isInitialized) nowPlayingNotification.destroySelfAndPlayer() if (this::nowPlayingNotification.isInitialized) nowPlayingNotification.destroySelfAndPlayer()

View File

@ -193,7 +193,7 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
context?.hideKeyboard(view) context?.hideKeyboard(view)
// clear the playing queue // clear the playing queue
PlayingQueue.clear() PlayingQueue.resetToDefaults()
changeOrientationMode() changeOrientationMode()
@ -502,7 +502,6 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
super.onDestroy() super.onDestroy()
try { try {
// clear the playing queue // clear the playing queue
PlayingQueue.clear()
PlayingQueue.resetToDefaults() PlayingQueue.resetToDefaults()
saveWatchPosition() saveWatchPosition()
@ -520,15 +519,19 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
// save the watch position if video isn't finished and option enabled // save the watch position if video isn't finished and option enabled
private fun saveWatchPosition() { private fun saveWatchPosition() {
if (PlayerHelper.watchPositionsEnabled && exoPlayer.currentPosition != exoPlayer.duration) { if (!PlayerHelper.watchPositionsEnabled) return
if (exoPlayer.currentPosition != exoPlayer.duration) {
val watchPosition = WatchPosition(videoId!!, exoPlayer.currentPosition)
query { query {
Database.watchPositionDao().insertAll(WatchPosition(videoId!!, exoPlayer.currentPosition)) Database.watchPositionDao().insertAll(watchPosition)
} }
} else if (PlayerHelper.watchPositionsEnabled) { } else if (PlayerHelper.watchPositionsEnabled) {
// delete watch position if video has ended // delete watch position if video has ended
query {
Database.watchPositionDao().deleteById(videoId!!) Database.watchPositionDao().deleteById(videoId!!)
} }
} }
}
private fun checkForSegments() { private fun checkForSegments() {
if (!exoPlayer.isPlaying || !PlayerHelper.sponsorBlockEnabled) return if (!exoPlayer.isPlaying || !PlayerHelper.sponsorBlockEnabled) return
@ -845,8 +848,9 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
// save the watch position when paused // save the watch position when paused
if (playbackState == PlaybackState.STATE_PAUSED) { if (playbackState == PlaybackState.STATE_PAUSED) {
val watchPosition = WatchPosition(videoId!!, exoPlayer.currentPosition)
query { query {
Database.watchPositionDao().insertAll(WatchPosition(videoId!!, exoPlayer.currentPosition)) Database.watchPositionDao().insertAll(watchPosition)
} }
} }

View File

@ -213,17 +213,17 @@ class NowPlayingNotification(
* Destroy the [NowPlayingNotification] * Destroy the [NowPlayingNotification]
*/ */
fun destroySelfAndPlayer() { fun destroySelfAndPlayer() {
playerNotification?.setPlayer(null)
mediaSession.isActive = false mediaSession.isActive = false
mediaSession.release() mediaSession.release()
mediaSessionConnector.setPlayer(null)
playerNotification?.setPlayer(null) player.stop()
player.release()
val notificationManager = context.getSystemService( val notificationManager = context.getSystemService(
Context.NOTIFICATION_SERVICE Context.NOTIFICATION_SERVICE
) as NotificationManager ) as NotificationManager
notificationManager.cancel(PLAYER_NOTIFICATION_ID) notificationManager.cancel(PLAYER_NOTIFICATION_ID)
player.stop()
player.release()
} }
} }

View File

@ -61,8 +61,6 @@ object PlayingQueue {
fun isEmpty() = queue.isEmpty() fun isEmpty() = queue.isEmpty()
fun clear() = queue.clear()
fun size() = queue.size fun size() = queue.size
fun currentIndex(): Int { fun currentIndex(): Int {
@ -139,5 +137,6 @@ object PlayingQueue {
fun resetToDefaults() { fun resetToDefaults() {
repeatQueue = false repeatQueue = false
onQueueTapListener = {} onQueueTapListener = {}
queue.clear()
} }
} }