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 662e29396..93f77bcca 100644 --- a/app/src/main/java/com/github/libretube/helpers/BackgroundHelper.kt +++ b/app/src/main/java/com/github/libretube/helpers/BackgroundHelper.kt @@ -4,8 +4,11 @@ import android.app.ActivityManager import android.content.Context import android.content.Intent import androidx.core.content.ContextCompat +import androidx.fragment.app.commit import com.github.libretube.constants.IntentData import com.github.libretube.services.BackgroundMode +import com.github.libretube.ui.activities.MainActivity +import com.github.libretube.ui.fragments.PlayerFragment /** * Helper for starting a new Instance of the [BackgroundMode] @@ -22,8 +25,18 @@ object BackgroundHelper { position: Long? = null, playlistId: String? = null, channelId: String? = null, - keepQueue: Boolean? = null + keepQueue: Boolean? = null, + keepVideoPlayerAlive: Boolean = false ) { + // close the previous video player if open + if (!keepVideoPlayerAlive) { + (context as? MainActivity)?.supportFragmentManager?.let { fragmentManager -> + fragmentManager.fragments.firstOrNull { it is PlayerFragment }?.let { + fragmentManager.commit { remove(it) } + } + } + } + // create an intent for the background mode service val intent = Intent(context, BackgroundMode::class.java) intent.putExtra(IntentData.videoId, videoId) 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 bcb8dc685..dba9225d9 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 @@ -71,6 +71,7 @@ class AudioPlayerFragment : Fragment(), AudioPlayerOptions { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) + audioHelper = AudioHelper(requireContext()) Intent(activity, BackgroundMode::class.java).also { intent -> activity?.bindService(intent, connection, Context.BIND_AUTO_CREATE) 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 dc56c6c86..f807ee4c8 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 @@ -465,7 +465,8 @@ class PlayerFragment : Fragment(R.layout.fragment_player), OnlinePlayerOptions { exoPlayer.currentPosition, playlistId, channelId, - true + keepQueue = true, + keepVideoPlayerAlive = true ) handler.postDelayed(500) { NavigationHelper.startAudioPlayer(requireContext())