fix: can't open audio player via notification in downloads

This commit is contained in:
Bnyro 2024-10-16 10:58:51 +02:00
parent 00122d27c4
commit 21d4e03509
6 changed files with 26 additions and 15 deletions

View File

@ -8,7 +8,6 @@ import android.os.Binder
import android.os.Handler
import android.os.IBinder
import android.os.Looper
import android.util.Log
import android.widget.Toast
import androidx.annotation.OptIn
import androidx.core.app.NotificationCompat
@ -124,6 +123,8 @@ abstract class AbstractPlayerService : LifecycleService() {
}
}
abstract val isOfflinePlayer: Boolean
override fun onCreate() {
super.onCreate()
@ -178,7 +179,9 @@ abstract class AbstractPlayerService : LifecycleService() {
nowPlayingNotification = NowPlayingNotification(
this,
player!!
player!!,
backgroundOnly = true,
offlinePlayer = isOfflinePlayer
)
}

View File

@ -30,6 +30,8 @@ import kotlin.io.path.exists
*/
@UnstableApi
class OfflinePlayerService : AbstractPlayerService() {
override val isOfflinePlayer: Boolean = true
private var downloadWithItems: DownloadWithItems? = null
private lateinit var downloadTab: DownloadTab
private var shuffle: Boolean = false

View File

@ -34,9 +34,9 @@ import kotlinx.serialization.encodeToString
*/
@androidx.annotation.OptIn(androidx.media3.common.util.UnstableApi::class)
class OnlinePlayerService : AbstractPlayerService() {
/**
* PlaylistId/ChannelId for autoplay
*/
override val isOfflinePlayer: Boolean = false
// PlaylistId/ChannelId for autoplay
private var playlistId: String? = null
private var channelId: String? = null
private var startTimestamp: Long? = null
@ -47,10 +47,8 @@ class OnlinePlayerService : AbstractPlayerService() {
var streams: Streams? = null
private set
/**
* SponsorBlock Segment data
*/
private var segments = listOf<Segment>()
// SponsorBlock Segment data
private var sponsorBlockSegments = listOf<Segment>()
private var sponsorBlockConfig = PlayerHelper.getSponsorBlockCategories()
override suspend fun onServiceCreated(intent: Intent) {
@ -159,7 +157,7 @@ class OnlinePlayerService : AbstractPlayerService() {
// play new video on background
this.videoId = nextVideo
this.streams = null
this.segments = emptyList()
this.sponsorBlockSegments = emptyList()
lifecycleScope.launch {
startPlaybackAndUpdateNotification()
@ -195,7 +193,7 @@ class OnlinePlayerService : AbstractPlayerService() {
lifecycleScope.launch(Dispatchers.IO) {
runCatching {
if (sponsorBlockConfig.isEmpty()) return@runCatching
segments = RetrofitInstance.api.getSegments(
sponsorBlockSegments = RetrofitInstance.api.getSegments(
videoId,
JsonHelper.json.encodeToString(sponsorBlockConfig.keys)
).segments
@ -210,7 +208,7 @@ class OnlinePlayerService : AbstractPlayerService() {
private fun checkForSegments() {
handler.postDelayed(this::checkForSegments, 100)
player?.checkForSegments(this, segments, sponsorBlockConfig)
player?.checkForSegments(this, sponsorBlockSegments, sponsorBlockConfig)
}
override fun onPlaybackStateChanged(playbackState: Int) {

View File

@ -465,7 +465,8 @@ class MainActivity : BaseActivity() {
}
if (intent?.getBooleanExtra(IntentData.openAudioPlayer, false) == true) {
NavigationHelper.startAudioPlayer(this)
val offlinePlayer = intent!!.getBooleanExtra(IntentData.offlinePlayer, false)
NavigationHelper.startAudioPlayer(this, offlinePlayer = offlinePlayer)
return
}

View File

@ -219,7 +219,8 @@ class OfflinePlayerActivity : BaseActivity() {
nowPlayingNotification = NowPlayingNotification(
this,
viewModel.player
viewModel.player,
offlinePlayer = true
)
}

View File

@ -33,7 +33,9 @@ import java.util.UUID
@androidx.annotation.OptIn(androidx.media3.common.util.UnstableApi::class)
class NowPlayingNotification(
private val context: Context,
private val player: ExoPlayer
private val player: ExoPlayer,
private val backgroundOnly: Boolean = false,
private val offlinePlayer: Boolean = false
) {
private var videoId: String? = null
private val nManager = context.getSystemService<NotificationManager>()!!
@ -73,11 +75,15 @@ class NowPlayingNotification(
// it doesn't start a completely new MainActivity because the MainActivity's launchMode
// is set to "singleTop" in the AndroidManifest (important!!!)
// that's the only way to launch back into the previous activity (e.g. the player view
if (!backgroundOnly) return null
val intent = Intent(context, MainActivity::class.java).apply {
putExtra(IntentData.openAudioPlayer, true)
putExtra(IntentData.offlinePlayer, offlinePlayer)
addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
}
return PendingIntentCompat
.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT, false)
}