11package com.github.libretube.helpers
22
3- import android.app.ActivityManager
43import android.content.ComponentName
54import android.content.Context
65import android.content.Intent
76import android.os.Bundle
87import androidx.annotation.OptIn
9- import androidx.core.content.getSystemService
108import androidx.core.os.bundleOf
119import androidx.fragment.app.commit
1210import androidx.media3.common.util.UnstableApi
@@ -38,19 +36,17 @@ object BackgroundHelper {
3836 playlistId : String? = null,
3937 channelId : String? = null,
4038 keepQueue : Boolean = false,
41- keepVideoPlayerAlive : Boolean = false
4239 ) {
4340 // close the previous video player if open
44- if (! keepVideoPlayerAlive) {
45- val fragmentManager =
46- ContextHelper .unwrapActivity<MainActivity >(context).supportFragmentManager
47- fragmentManager.fragments.firstOrNull { it is PlayerFragment }?.let {
48- fragmentManager.commit { remove(it) }
49- }
41+ val fragmentManager =
42+ ContextHelper .unwrapActivity<MainActivity >(context).supportFragmentManager
43+ fragmentManager.fragments.firstOrNull { it is PlayerFragment }?.let {
44+ fragmentManager.commit { remove(it) }
5045 }
5146
5247 val playerData = PlayerData (videoId, playlistId, channelId, keepQueue, position)
5348
49+ stopBackgroundPlay(context)
5450 startMediaService(
5551 context,
5652 OnlinePlayerService ::class .java,
@@ -71,18 +67,6 @@ object BackgroundHelper {
7167 }
7268 }
7369
74- /* *
75- * Check if the [OnlinePlayerService] service is currently running.
76- */
77- fun isBackgroundServiceRunning (
78- context : Context ,
79- serviceClass : Class <* > = OnlinePlayerService : :class.java
80- ): Boolean {
81- @Suppress(" DEPRECATION" )
82- return context.getSystemService<ActivityManager >()!! .getRunningServices(Int .MAX_VALUE )
83- .any { serviceClass.name == it.service.className }
84- }
85-
8670 /* *
8771 * Start the offline background player
8872 *
@@ -106,6 +90,7 @@ object BackgroundHelper {
10690 IntentData .audioOnly to true
10791 )
10892
93+ stopBackgroundPlay(context)
10994 startMediaService(context, OfflinePlayerService ::class .java, arguments)
11095 }
11196
0 commit comments