Skip to content

Commit

Permalink
Merge pull request #6784 from Bnyro/master
Browse files Browse the repository at this point in the history
fix: crash when starting other video
  • Loading branch information
Bnyro authored Nov 19, 2024
2 parents 9286bbe + 994bebd commit 952c9ad
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.os.Handler
import android.os.Looper
import androidx.annotation.OptIn
import androidx.core.app.ServiceCompat
import androidx.core.os.postDelayed
import androidx.media3.common.C
import androidx.media3.common.PlaybackException
import androidx.media3.common.Player
Expand Down Expand Up @@ -293,29 +294,36 @@ abstract class AbstractPlayerService : MediaLibraryService(), MediaLibrarySessio
}

override fun onDestroy() {
PlayingQueue.resetToDefaults()
// wait for a short time before killing the mediaSession
// as the playerController must be released before we finish the session
// otherwise there would be a
// java.lang.SecurityException: Session rejected the connection request.
// because there can't be two active playerControllers at the same time.
handler.postDelayed(50) {
PlayingQueue.resetToDefaults()

saveWatchPosition()
saveWatchPosition()

notificationProvider = null
watchPositionTimer.destroy()
notificationProvider = null
watchPositionTimer.destroy()

handler.removeCallbacksAndMessages(null)
handler.removeCallbacksAndMessages(null)

runCatching {
exoPlayer?.stop()
exoPlayer?.release()
}
runCatching {
exoPlayer?.stop()
exoPlayer?.release()
}

kotlin.runCatching {
mediaLibrarySession?.release()
mediaLibrarySession = null
}
kotlin.runCatching {
mediaLibrarySession?.release()
mediaLibrarySession = null
}

ServiceCompat.stopForeground(this, ServiceCompat.STOP_FOREGROUND_REMOVE)
stopSelf()
ServiceCompat.stopForeground(this, ServiceCompat.STOP_FOREGROUND_REMOVE)
stopSelf()

super.onDestroy()
super.onDestroy()
}
}

fun isVideoIdInitialized() = this::videoId.isInitialized
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,8 @@ class AudioPlayerFragment : Fragment(), AudioPlayerOptions {

private fun killFragment() {
playerController?.sendCustomCommand(AbstractPlayerService.stopServiceCommand, Bundle.EMPTY)
playerController?.release()
playerController = null

viewModel.isFullscreen.value = false
binding.playerMotionLayout.transitionToEnd()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -851,6 +851,7 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
playerController.pause()

playerController.sendCustomCommand(AbstractPlayerService.stopServiceCommand, Bundle.EMPTY)
playerController.release()

if (PlayerHelper.pipEnabled) {
// disable the auto PiP mode for SDK >= 32
Expand Down

0 comments on commit 952c9ad

Please sign in to comment.