mirror of
https://github.com/libre-tube/LibreTube.git
synced 2025-04-27 15:30:31 +05:30
Merge pull request #1914 from Bnyro/master
Fix crash on destroy of the player notification
This commit is contained in:
commit
19dd4a7a56
@ -9,7 +9,6 @@ import android.os.Build
|
||||
import android.os.Handler
|
||||
import android.os.IBinder
|
||||
import android.os.Looper
|
||||
import android.util.Log
|
||||
import android.widget.Toast
|
||||
import com.fasterxml.jackson.databind.ObjectMapper
|
||||
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.PLAYER_NOTIFICATION_ID
|
||||
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.obj.WatchPosition
|
||||
import com.github.libretube.extensions.awaitQuery
|
||||
@ -117,7 +115,7 @@ class BackgroundMode : Service() {
|
||||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
||||
try {
|
||||
// clear the playing queue
|
||||
PlayingQueue.clear()
|
||||
PlayingQueue.resetToDefaults()
|
||||
|
||||
// get the intent arguments
|
||||
videoId = intent?.getStringExtra(IntentData.videoId)!!
|
||||
@ -201,9 +199,8 @@ class BackgroundMode : Service() {
|
||||
} else if (PlayerHelper.watchPositionsEnabled) {
|
||||
try {
|
||||
val watchPosition = awaitQuery {
|
||||
DatabaseHolder.Database.watchPositionDao().findById(videoId)
|
||||
Database.watchPositionDao().findById(videoId)
|
||||
}
|
||||
Log.e("position", watchPosition.toString())
|
||||
streams?.duration?.let {
|
||||
if (watchPosition != null && watchPosition.position < it * 1000 * 0.9) {
|
||||
player?.seekTo(watchPosition.position)
|
||||
@ -355,7 +352,7 @@ class BackgroundMode : Service() {
|
||||
*/
|
||||
override fun onDestroy() {
|
||||
// clear the playing queue
|
||||
PlayingQueue.clear()
|
||||
PlayingQueue.resetToDefaults()
|
||||
|
||||
if (this::nowPlayingNotification.isInitialized) nowPlayingNotification.destroySelfAndPlayer()
|
||||
|
||||
|
@ -193,7 +193,7 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
|
||||
context?.hideKeyboard(view)
|
||||
|
||||
// clear the playing queue
|
||||
PlayingQueue.clear()
|
||||
PlayingQueue.resetToDefaults()
|
||||
|
||||
changeOrientationMode()
|
||||
|
||||
@ -502,7 +502,6 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
|
||||
super.onDestroy()
|
||||
try {
|
||||
// clear the playing queue
|
||||
PlayingQueue.clear()
|
||||
PlayingQueue.resetToDefaults()
|
||||
|
||||
saveWatchPosition()
|
||||
@ -520,15 +519,19 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
|
||||
|
||||
// save the watch position if video isn't finished and option enabled
|
||||
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 {
|
||||
Database.watchPositionDao().insertAll(WatchPosition(videoId!!, exoPlayer.currentPosition))
|
||||
Database.watchPositionDao().insertAll(watchPosition)
|
||||
}
|
||||
} else if (PlayerHelper.watchPositionsEnabled) {
|
||||
// delete watch position if video has ended
|
||||
query {
|
||||
Database.watchPositionDao().deleteById(videoId!!)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun checkForSegments() {
|
||||
if (!exoPlayer.isPlaying || !PlayerHelper.sponsorBlockEnabled) return
|
||||
@ -845,8 +848,9 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
|
||||
|
||||
// save the watch position when paused
|
||||
if (playbackState == PlaybackState.STATE_PAUSED) {
|
||||
val watchPosition = WatchPosition(videoId!!, exoPlayer.currentPosition)
|
||||
query {
|
||||
Database.watchPositionDao().insertAll(WatchPosition(videoId!!, exoPlayer.currentPosition))
|
||||
Database.watchPositionDao().insertAll(watchPosition)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -213,17 +213,17 @@ class NowPlayingNotification(
|
||||
* Destroy the [NowPlayingNotification]
|
||||
*/
|
||||
fun destroySelfAndPlayer() {
|
||||
playerNotification?.setPlayer(null)
|
||||
|
||||
mediaSession.isActive = false
|
||||
mediaSession.release()
|
||||
mediaSessionConnector.setPlayer(null)
|
||||
playerNotification?.setPlayer(null)
|
||||
|
||||
player.stop()
|
||||
player.release()
|
||||
|
||||
val notificationManager = context.getSystemService(
|
||||
Context.NOTIFICATION_SERVICE
|
||||
) as NotificationManager
|
||||
notificationManager.cancel(PLAYER_NOTIFICATION_ID)
|
||||
|
||||
player.stop()
|
||||
player.release()
|
||||
}
|
||||
}
|
||||
|
@ -61,8 +61,6 @@ object PlayingQueue {
|
||||
|
||||
fun isEmpty() = queue.isEmpty()
|
||||
|
||||
fun clear() = queue.clear()
|
||||
|
||||
fun size() = queue.size
|
||||
|
||||
fun currentIndex(): Int {
|
||||
@ -139,5 +137,6 @@ object PlayingQueue {
|
||||
fun resetToDefaults() {
|
||||
repeatQueue = false
|
||||
onQueueTapListener = {}
|
||||
queue.clear()
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user