Properly destroy the background mode service

This commit is contained in:
Bnyro 2022-10-18 19:40:28 +02:00
parent 9998cec4c4
commit b1362744fc
3 changed files with 7 additions and 4 deletions

View File

@ -14,6 +14,7 @@ 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
import com.github.libretube.api.RetrofitInstance import com.github.libretube.api.RetrofitInstance
import com.github.libretube.api.obj.Segment
import com.github.libretube.api.obj.Segments import com.github.libretube.api.obj.Segments
import com.github.libretube.api.obj.Streams import com.github.libretube.api.obj.Streams
import com.github.libretube.constants.BACKGROUND_CHANNEL_ID import com.github.libretube.constants.BACKGROUND_CHANNEL_ID
@ -343,7 +344,7 @@ class BackgroundMode : Service() {
if (segmentData == null || segmentData!!.segments.isEmpty()) return if (segmentData == null || segmentData!!.segments.isEmpty()) return
segmentData!!.segments.forEach { segment: com.github.libretube.api.obj.Segment -> segmentData!!.segments.forEach { segment: Segment ->
val segmentStart = (segment.segment!![0] * 1000f).toLong() val segmentStart = (segment.segment!![0] * 1000f).toLong()
val segmentEnd = (segment.segment[1] * 1000f).toLong() val segmentEnd = (segment.segment[1] * 1000f).toLong()
val currentPosition = player?.currentPosition val currentPosition = player?.currentPosition
@ -368,6 +369,8 @@ class BackgroundMode : Service() {
// clear the playing queue // clear the playing queue
PlayingQueue.clear() PlayingQueue.clear()
if (this::nowPlayingNotification.isInitialized) nowPlayingNotification.destroySelfAndPlayer()
// called when the user pressed stop in the notification // called when the user pressed stop in the notification
// stop the service from being in the foreground and remove the notification // stop the service from being in the foreground and remove the notification
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
@ -378,7 +381,7 @@ class BackgroundMode : Service() {
} }
// destroy the service // destroy the service
stopSelf() stopSelf()
if (this::nowPlayingNotification.isInitialized) nowPlayingNotification.destroy()
super.onDestroy() super.onDestroy()
} }

View File

@ -561,7 +561,7 @@ class PlayerFragment : BaseFragment() {
PlayingQueue.clear() PlayingQueue.clear()
saveWatchPosition() saveWatchPosition()
nowPlayingNotification.destroy() nowPlayingNotification.destroySelfAndPlayer()
activity?.requestedOrientation = activity?.requestedOrientation =
if ((activity as MainActivity).autoRotationEnabled) { if ((activity as MainActivity).autoRotationEnabled) {
ActivityInfo.SCREEN_ORIENTATION_USER ActivityInfo.SCREEN_ORIENTATION_USER

View File

@ -169,7 +169,7 @@ class NowPlayingNotification(
/** /**
* Destroy the [NowPlayingNotification] * Destroy the [NowPlayingNotification]
*/ */
fun destroy() { fun destroySelfAndPlayer() {
mediaSession.isActive = false mediaSession.isActive = false
mediaSession.release() mediaSession.release()
mediaSessionConnector.setPlayer(null) mediaSessionConnector.setPlayer(null)