mirror of
https://github.com/libre-tube/LibreTube.git
synced 2025-04-29 16:30:31 +05:30
Merge pull request #6773 from Bnyro/master
fix: properly kill playback service and notification on player close
This commit is contained in:
commit
92d3475aa4
@ -119,8 +119,6 @@ object BackgroundHelper {
|
||||
arguments: Bundle,
|
||||
onController: (MediaController) -> Unit = {}
|
||||
) {
|
||||
stopBackgroundPlay(context)
|
||||
|
||||
val sessionToken =
|
||||
SessionToken(context, ComponentName(context, serviceClass))
|
||||
|
||||
|
@ -59,7 +59,6 @@ object NavigationHelper {
|
||||
forceVideo: Boolean = false
|
||||
) {
|
||||
if (videoUrlOrId == null) return
|
||||
BackgroundHelper.stopBackgroundPlay(context)
|
||||
|
||||
if (PreferenceHelper.getBoolean(PreferenceKeys.AUDIO_ONLY_MODE, false) && !forceVideo) {
|
||||
BackgroundHelper.playOnBackground(
|
||||
|
@ -89,27 +89,28 @@ abstract class AbstractPlayerService : MediaLibraryService(), MediaLibrarySessio
|
||||
customCommand: SessionCommand,
|
||||
args: Bundle
|
||||
): ListenableFuture<SessionResult> {
|
||||
if (customCommand.customAction == START_SERVICE_ACTION) {
|
||||
PlayingQueue.resetToDefaults()
|
||||
when (customCommand.customAction) {
|
||||
START_SERVICE_ACTION -> {
|
||||
PlayingQueue.resetToDefaults()
|
||||
|
||||
CoroutineScope(Dispatchers.IO).launch {
|
||||
onServiceCreated(args)
|
||||
notificationProvider?.intentActivity = getIntentActivity()
|
||||
CoroutineScope(Dispatchers.IO).launch {
|
||||
onServiceCreated(args)
|
||||
notificationProvider?.intentActivity = getIntentActivity()
|
||||
|
||||
startPlayback()
|
||||
startPlayback()
|
||||
}
|
||||
}
|
||||
STOP_SERVICE_ACTION -> {
|
||||
onDestroy()
|
||||
}
|
||||
RUN_PLAYER_COMMAND_ACTION -> {
|
||||
runPlayerCommand(args)
|
||||
}
|
||||
else -> {
|
||||
handlePlayerAction(PlayerEvent.valueOf(customCommand.customAction))
|
||||
}
|
||||
|
||||
return super.onCustomCommand(session, controller, customCommand, args)
|
||||
}
|
||||
|
||||
if (customCommand.customAction == RUN_PLAYER_COMMAND_ACTION) {
|
||||
runPlayerCommand(args)
|
||||
|
||||
return super.onCustomCommand(session, controller, customCommand, args)
|
||||
}
|
||||
|
||||
handlePlayerAction(PlayerEvent.valueOf(customCommand.customAction))
|
||||
|
||||
return super.onCustomCommand(session, controller, customCommand, args)
|
||||
}
|
||||
|
||||
@ -238,11 +239,8 @@ abstract class AbstractPlayerService : MediaLibraryService(), MediaLibrarySessio
|
||||
val mediaNotificationSessionCommands =
|
||||
connectionResult.availableSessionCommands.buildUpon()
|
||||
.also { builder ->
|
||||
builder.add(startServiceCommand)
|
||||
builder.add(runPlayerActionCommand)
|
||||
customLayout.forEach { commandButton ->
|
||||
commandButton.sessionCommand?.let { builder.add(it) }
|
||||
}
|
||||
builder.addSessionCommands(listOf(startServiceCommand, runPlayerActionCommand, stopServiceCommand))
|
||||
builder.addSessionCommands(customLayout.mapNotNull(CommandButton::sessionCommand))
|
||||
}
|
||||
.build()
|
||||
|
||||
@ -332,9 +330,11 @@ abstract class AbstractPlayerService : MediaLibraryService(), MediaLibrarySessio
|
||||
|
||||
companion object {
|
||||
private const val START_SERVICE_ACTION = "start_service_action"
|
||||
private const val STOP_SERVICE_ACTION = "stop_service_action"
|
||||
private const val RUN_PLAYER_COMMAND_ACTION = "run_player_command_action"
|
||||
|
||||
val startServiceCommand = SessionCommand(START_SERVICE_ACTION, Bundle.EMPTY)
|
||||
val stopServiceCommand = SessionCommand(STOP_SERVICE_ACTION, Bundle.EMPTY)
|
||||
val runPlayerActionCommand = SessionCommand(RUN_PLAYER_COMMAND_ACTION, Bundle.EMPTY)
|
||||
}
|
||||
}
|
||||
|
@ -6,6 +6,7 @@ import android.content.Intent
|
||||
import android.os.IBinder
|
||||
import androidx.core.content.getSystemService
|
||||
import com.github.libretube.enums.NotificationId
|
||||
import com.github.libretube.helpers.BackgroundHelper
|
||||
|
||||
class OnClearFromRecentService : Service() {
|
||||
private var nManager: NotificationManager? = null
|
||||
@ -22,6 +23,7 @@ class OnClearFromRecentService : Service() {
|
||||
}
|
||||
|
||||
override fun onTaskRemoved(rootIntent: Intent?) {
|
||||
BackgroundHelper.stopBackgroundPlay(this)
|
||||
nManager?.cancel(NotificationId.PLAYER_PLAYBACK.id)
|
||||
stopSelf()
|
||||
}
|
||||
|
@ -230,6 +230,8 @@ class OfflinePlayerActivity : BaseActivity() {
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
playerController.sendCustomCommand(AbstractPlayerService.stopServiceCommand, Bundle.EMPTY)
|
||||
|
||||
runCatching {
|
||||
unregisterReceiver(playerActionReceiver)
|
||||
}
|
||||
|
@ -7,7 +7,6 @@ import android.os.Bundle
|
||||
import android.os.Handler
|
||||
import android.os.Looper
|
||||
import android.text.format.DateUtils
|
||||
import android.util.Log
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
@ -42,6 +41,7 @@ import com.github.libretube.helpers.NavBarHelper
|
||||
import com.github.libretube.helpers.NavigationHelper
|
||||
import com.github.libretube.helpers.PlayerHelper
|
||||
import com.github.libretube.helpers.ThemeHelper
|
||||
import com.github.libretube.services.AbstractPlayerService
|
||||
import com.github.libretube.services.OfflinePlayerService
|
||||
import com.github.libretube.services.OnlinePlayerService
|
||||
import com.github.libretube.ui.activities.MainActivity
|
||||
@ -170,8 +170,8 @@ class AudioPlayerFragment : Fragment(), AudioPlayerOptions {
|
||||
}
|
||||
|
||||
binding.openVideo.setOnClickListener {
|
||||
BackgroundHelper.stopBackgroundPlay(requireContext())
|
||||
killFragment()
|
||||
|
||||
NavigationHelper.navigateVideo(
|
||||
context = requireContext(),
|
||||
videoUrlOrId = PlayingQueue.getCurrent()?.url,
|
||||
@ -202,8 +202,6 @@ class AudioPlayerFragment : Fragment(), AudioPlayerOptions {
|
||||
}
|
||||
|
||||
binding.miniPlayerClose.setOnClickListener {
|
||||
playerController?.release()
|
||||
BackgroundHelper.stopBackgroundPlay(requireContext())
|
||||
killFragment()
|
||||
}
|
||||
|
||||
@ -233,6 +231,8 @@ class AudioPlayerFragment : Fragment(), AudioPlayerOptions {
|
||||
}
|
||||
|
||||
private fun killFragment() {
|
||||
playerController?.sendCustomCommand(AbstractPlayerService.stopServiceCommand, Bundle.EMPTY)
|
||||
|
||||
viewModel.isFullscreen.value = false
|
||||
binding.playerMotionLayout.transitionToEnd()
|
||||
activity.supportFragmentManager.commit {
|
||||
|
@ -238,11 +238,6 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
|
||||
PictureInPictureCompat.setPictureInPictureParams(requireActivity(), pipParams)
|
||||
}
|
||||
|
||||
if (isPlaying) {
|
||||
// Stop [BackgroundMode] service if it is running.
|
||||
BackgroundHelper.stopBackgroundPlay(requireContext())
|
||||
}
|
||||
|
||||
if (isPlaying && PlayerHelper.sponsorBlockEnabled) {
|
||||
handler.postDelayed(
|
||||
this@PlayerFragment::checkForSegments,
|
||||
@ -567,23 +562,17 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
|
||||
.animateDown(
|
||||
duration = 300L,
|
||||
dy = 500F,
|
||||
onEnd = ::onManualPlayerClose
|
||||
onEnd = ::killPlayerFragment
|
||||
)
|
||||
}
|
||||
|
||||
private fun onManualPlayerClose() {
|
||||
PlayingQueue.clear()
|
||||
BackgroundHelper.stopBackgroundPlay(requireContext())
|
||||
killPlayerFragment()
|
||||
}
|
||||
|
||||
// actions that don't depend on video information
|
||||
private fun initializeOnClickActions() {
|
||||
binding.closeImageView.setOnClickListener {
|
||||
onManualPlayerClose()
|
||||
killPlayerFragment()
|
||||
}
|
||||
playerBinding.closeImageButton.setOnClickListener {
|
||||
onManualPlayerClose()
|
||||
killPlayerFragment()
|
||||
}
|
||||
|
||||
binding.playImageView.setOnClickListener {
|
||||
@ -736,7 +725,6 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
|
||||
}
|
||||
|
||||
private fun playOnBackground() {
|
||||
BackgroundHelper.stopBackgroundPlay(requireContext())
|
||||
BackgroundHelper.playOnBackground(
|
||||
requireContext(),
|
||||
videoId,
|
||||
@ -878,6 +866,8 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
|
||||
playerController.removeListener(playerListener)
|
||||
playerController.pause()
|
||||
|
||||
playerController.sendCustomCommand(AbstractPlayerService.stopServiceCommand, Bundle.EMPTY)
|
||||
|
||||
if (PlayerHelper.pipEnabled) {
|
||||
// disable the auto PiP mode for SDK >= 32
|
||||
PictureInPictureCompat
|
||||
|
Loading…
x
Reference in New Issue
Block a user