Skip to content

Commit b3c2cd0

Browse files
authored
Merge pull request libre-tube#7270 from Bnyro/master
fix: background shuffle mode doesn't builder a proper queue
2 parents ff87844 + bf07874 commit b3c2cd0

5 files changed

Lines changed: 12 additions & 53 deletions

File tree

app/src/main/AndroidManifest.xml

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -434,30 +434,6 @@
434434

435435
</service>
436436

437-
<service
438-
android:name=".services.VideoOnlinePlayerService"
439-
android:enabled="true"
440-
android:exported="false"
441-
android:foregroundServiceType="mediaPlayback">
442-
443-
<intent-filter>
444-
<action android:name="androidx.media3.session.MediaSessionService"/>
445-
</intent-filter>
446-
447-
</service>
448-
449-
<service
450-
android:name=".services.VideoOfflinePlayerService"
451-
android:enabled="true"
452-
android:exported="false"
453-
android:foregroundServiceType="mediaPlayback">
454-
455-
<intent-filter>
456-
<action android:name="androidx.media3.session.MediaSessionService"/>
457-
</intent-filter>
458-
459-
</service>
460-
461437
<service
462438
android:name=".services.OnClearFromRecentService"
463439
android:enabled="true"

app/src/main/java/com/github/libretube/db/dao/DownloadDao.kt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@ interface DownloadDao {
2626
@Query("SELECT EXISTS (SELECT * FROM download WHERE videoId = :videoId)")
2727
suspend fun exists(videoId: String): Boolean
2828

29-
@Query("SELECT videoId FROM downloadItem WHERE type = :fileType ORDER BY RANDOM() LIMIT 1")
30-
suspend fun getRandomVideoIdByFileType(fileType: FileType): String?
31-
3229
@Query("SELECT * FROM downloaditem WHERE id = :id")
3330
suspend fun findDownloadItemById(id: Int): DownloadItem?
3431

app/src/main/java/com/github/libretube/helpers/BackgroundHelper.kt

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
package com.github.libretube.helpers
22

3-
import android.app.ActivityManager
43
import android.content.ComponentName
54
import android.content.Context
65
import android.content.Intent
76
import android.os.Bundle
87
import androidx.annotation.OptIn
9-
import androidx.core.content.getSystemService
108
import androidx.core.os.bundleOf
119
import androidx.fragment.app.commit
1210
import 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

app/src/main/java/com/github/libretube/services/OfflinePlayerService.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,12 @@ open class OfflinePlayerService : AbstractPlayerService() {
7575
noInternetService = args.getBoolean(IntentData.noInternet, false)
7676
isAudioOnlyPlayer = args.getBoolean(IntentData.audioOnly, false)
7777

78+
PlayingQueue.clear()
79+
7880
val videoId = if (shuffle) {
7981
runBlocking(Dispatchers.IO) {
80-
Database.downloadDao().getRandomVideoIdByFileType(FileType.AUDIO)
81-
}
82+
Database.downloadDao().getAll().filterByTab(downloadTab).randomOrNull()
83+
}?.download?.videoId
8284
} else {
8385
args.getString(IntentData.videoId)
8486
} ?: return
@@ -89,7 +91,6 @@ open class OfflinePlayerService : AbstractPlayerService() {
8991
setTrackTypeDisabled(C.TRACK_TYPE_VIDEO, isAudioOnlyPlayer)
9092
}
9193

92-
PlayingQueue.clear()
9394
fillQueue()
9495
}
9596

@@ -203,6 +204,7 @@ open class OfflinePlayerService : AbstractPlayerService() {
203204
Database.downloadDao().getAll()
204205
}
205206
.filterByTab(downloadTab)
207+
.filter { it.download.videoId != videoId }
206208
.toMutableList()
207209

208210
if (shuffle) downloads.shuffle()

app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1481,8 +1481,7 @@ class PlayerFragment : Fragment(R.layout.fragment_player), OnlinePlayerOptions {
14811481
}
14821482

14831483
private fun shouldStartPiP(): Boolean {
1484-
return shouldUsePip() && ::playerController.isInitialized && playerController.isPlaying &&
1485-
!BackgroundHelper.isBackgroundServiceRunning(requireContext())
1484+
return shouldUsePip() && ::playerController.isInitialized && playerController.isPlaying
14861485
}
14871486

14881487
/**

0 commit comments

Comments
 (0)