fix: background shuffle mode doesn't builder a proper queue

This commit is contained in:
Bnyro 2025-04-01 10:55:04 +02:00
parent 763f90f226
commit bf07874ef9
No known key found for this signature in database
5 changed files with 12 additions and 53 deletions

View File

@ -434,30 +434,6 @@
</service> </service>
<service
android:name=".services.VideoOnlinePlayerService"
android:enabled="true"
android:exported="false"
android:foregroundServiceType="mediaPlayback">
<intent-filter>
<action android:name="androidx.media3.session.MediaSessionService"/>
</intent-filter>
</service>
<service
android:name=".services.VideoOfflinePlayerService"
android:enabled="true"
android:exported="false"
android:foregroundServiceType="mediaPlayback">
<intent-filter>
<action android:name="androidx.media3.session.MediaSessionService"/>
</intent-filter>
</service>
<service <service
android:name=".services.OnClearFromRecentService" android:name=".services.OnClearFromRecentService"
android:enabled="true" android:enabled="true"

View File

@ -26,9 +26,6 @@ interface DownloadDao {
@Query("SELECT EXISTS (SELECT * FROM download WHERE videoId = :videoId)") @Query("SELECT EXISTS (SELECT * FROM download WHERE videoId = :videoId)")
suspend fun exists(videoId: String): Boolean suspend fun exists(videoId: String): Boolean
@Query("SELECT videoId FROM downloadItem WHERE type = :fileType ORDER BY RANDOM() LIMIT 1")
suspend fun getRandomVideoIdByFileType(fileType: FileType): String?
@Query("SELECT * FROM downloaditem WHERE id = :id") @Query("SELECT * FROM downloaditem WHERE id = :id")
suspend fun findDownloadItemById(id: Int): DownloadItem? suspend fun findDownloadItemById(id: Int): DownloadItem?

View File

@ -1,12 +1,10 @@
package com.github.libretube.helpers package com.github.libretube.helpers
import android.app.ActivityManager
import android.content.ComponentName import android.content.ComponentName
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.os.Bundle import android.os.Bundle
import androidx.annotation.OptIn import androidx.annotation.OptIn
import androidx.core.content.getSystemService
import androidx.core.os.bundleOf import androidx.core.os.bundleOf
import androidx.fragment.app.commit import androidx.fragment.app.commit
import androidx.media3.common.util.UnstableApi import androidx.media3.common.util.UnstableApi
@ -38,19 +36,17 @@ object BackgroundHelper {
playlistId: String? = null, playlistId: String? = null,
channelId: String? = null, channelId: String? = null,
keepQueue: Boolean = false, keepQueue: Boolean = false,
keepVideoPlayerAlive: Boolean = false
) { ) {
// close the previous video player if open // close the previous video player if open
if (!keepVideoPlayerAlive) { val fragmentManager =
val fragmentManager = ContextHelper.unwrapActivity<MainActivity>(context).supportFragmentManager
ContextHelper.unwrapActivity<MainActivity>(context).supportFragmentManager fragmentManager.fragments.firstOrNull { it is PlayerFragment }?.let {
fragmentManager.fragments.firstOrNull { it is PlayerFragment }?.let { fragmentManager.commit { remove(it) }
fragmentManager.commit { remove(it) }
}
} }
val playerData = PlayerData(videoId, playlistId, channelId, keepQueue, position) val playerData = PlayerData(videoId, playlistId, channelId, keepQueue, position)
stopBackgroundPlay(context)
startMediaService( startMediaService(
context, context,
OnlinePlayerService::class.java, OnlinePlayerService::class.java,
@ -71,18 +67,6 @@ object BackgroundHelper {
} }
} }
/**
* Check if the [OnlinePlayerService] service is currently running.
*/
fun isBackgroundServiceRunning(
context: Context,
serviceClass: Class<*> = OnlinePlayerService::class.java
): Boolean {
@Suppress("DEPRECATION")
return context.getSystemService<ActivityManager>()!!.getRunningServices(Int.MAX_VALUE)
.any { serviceClass.name == it.service.className }
}
/** /**
* Start the offline background player * Start the offline background player
* *
@ -106,6 +90,7 @@ object BackgroundHelper {
IntentData.audioOnly to true IntentData.audioOnly to true
) )
stopBackgroundPlay(context)
startMediaService(context, OfflinePlayerService::class.java, arguments) startMediaService(context, OfflinePlayerService::class.java, arguments)
} }

View File

@ -75,10 +75,12 @@ open class OfflinePlayerService : AbstractPlayerService() {
noInternetService = args.getBoolean(IntentData.noInternet, false) noInternetService = args.getBoolean(IntentData.noInternet, false)
isAudioOnlyPlayer = args.getBoolean(IntentData.audioOnly, false) isAudioOnlyPlayer = args.getBoolean(IntentData.audioOnly, false)
PlayingQueue.clear()
val videoId = if (shuffle) { val videoId = if (shuffle) {
runBlocking(Dispatchers.IO) { runBlocking(Dispatchers.IO) {
Database.downloadDao().getRandomVideoIdByFileType(FileType.AUDIO) Database.downloadDao().getAll().filterByTab(downloadTab).randomOrNull()
} }?.download?.videoId
} else { } else {
args.getString(IntentData.videoId) args.getString(IntentData.videoId)
} ?: return } ?: return
@ -89,7 +91,6 @@ open class OfflinePlayerService : AbstractPlayerService() {
setTrackTypeDisabled(C.TRACK_TYPE_VIDEO, isAudioOnlyPlayer) setTrackTypeDisabled(C.TRACK_TYPE_VIDEO, isAudioOnlyPlayer)
} }
PlayingQueue.clear()
fillQueue() fillQueue()
} }
@ -203,6 +204,7 @@ open class OfflinePlayerService : AbstractPlayerService() {
Database.downloadDao().getAll() Database.downloadDao().getAll()
} }
.filterByTab(downloadTab) .filterByTab(downloadTab)
.filter { it.download.videoId != videoId }
.toMutableList() .toMutableList()
if (shuffle) downloads.shuffle() if (shuffle) downloads.shuffle()

View File

@ -1481,8 +1481,7 @@ class PlayerFragment : Fragment(R.layout.fragment_player), OnlinePlayerOptions {
} }
private fun shouldStartPiP(): Boolean { private fun shouldStartPiP(): Boolean {
return shouldUsePip() && ::playerController.isInitialized && playerController.isPlaying && return shouldUsePip() && ::playerController.isInitialized && playerController.isPlaying
!BackgroundHelper.isBackgroundServiceRunning(requireContext())
} }
/** /**