diff --git a/app/src/main/java/com/github/libretube/helpers/BackgroundHelper.kt b/app/src/main/java/com/github/libretube/helpers/BackgroundHelper.kt index aa8dd9b9f..f9af3dacf 100644 --- a/app/src/main/java/com/github/libretube/helpers/BackgroundHelper.kt +++ b/app/src/main/java/com/github/libretube/helpers/BackgroundHelper.kt @@ -119,8 +119,6 @@ object BackgroundHelper { arguments: Bundle, onController: (MediaController) -> Unit = {} ) { - stopBackgroundPlay(context) - val sessionToken = SessionToken(context, ComponentName(context, serviceClass)) diff --git a/app/src/main/java/com/github/libretube/helpers/NavigationHelper.kt b/app/src/main/java/com/github/libretube/helpers/NavigationHelper.kt index 0ce457262..92aa015ff 100644 --- a/app/src/main/java/com/github/libretube/helpers/NavigationHelper.kt +++ b/app/src/main/java/com/github/libretube/helpers/NavigationHelper.kt @@ -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( diff --git a/app/src/main/java/com/github/libretube/services/AbstractPlayerService.kt b/app/src/main/java/com/github/libretube/services/AbstractPlayerService.kt index 3c7e42c6a..f01daaa13 100644 --- a/app/src/main/java/com/github/libretube/services/AbstractPlayerService.kt +++ b/app/src/main/java/com/github/libretube/services/AbstractPlayerService.kt @@ -89,27 +89,28 @@ abstract class AbstractPlayerService : MediaLibraryService(), MediaLibrarySessio customCommand: SessionCommand, args: Bundle ): ListenableFuture { - 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) } } diff --git a/app/src/main/java/com/github/libretube/services/OnClearFromRecentService.kt b/app/src/main/java/com/github/libretube/services/OnClearFromRecentService.kt index bc9cff58f..61966e87e 100644 --- a/app/src/main/java/com/github/libretube/services/OnClearFromRecentService.kt +++ b/app/src/main/java/com/github/libretube/services/OnClearFromRecentService.kt @@ -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() } diff --git a/app/src/main/java/com/github/libretube/ui/activities/OfflinePlayerActivity.kt b/app/src/main/java/com/github/libretube/ui/activities/OfflinePlayerActivity.kt index b836b8d00..d578f78a0 100644 --- a/app/src/main/java/com/github/libretube/ui/activities/OfflinePlayerActivity.kt +++ b/app/src/main/java/com/github/libretube/ui/activities/OfflinePlayerActivity.kt @@ -230,6 +230,8 @@ class OfflinePlayerActivity : BaseActivity() { } override fun onDestroy() { + playerController.sendCustomCommand(AbstractPlayerService.stopServiceCommand, Bundle.EMPTY) + runCatching { unregisterReceiver(playerActionReceiver) } diff --git a/app/src/main/java/com/github/libretube/ui/fragments/AudioPlayerFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/AudioPlayerFragment.kt index b1eb9265c..65ae40b1e 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/AudioPlayerFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/AudioPlayerFragment.kt @@ -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 { diff --git a/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt index a9e14045e..f42f1ed19 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt @@ -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