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

View File

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

View File

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

View File

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

View File

@ -219,7 +219,8 @@ class OfflinePlayerActivity : BaseActivity() {
nowPlayingNotification = NowPlayingNotification( nowPlayingNotification = NowPlayingNotification(
this, 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) @androidx.annotation.OptIn(androidx.media3.common.util.UnstableApi::class)
class NowPlayingNotification( class NowPlayingNotification(
private val context: Context, 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 var videoId: String? = null
private val nManager = context.getSystemService<NotificationManager>()!! 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 // it doesn't start a completely new MainActivity because the MainActivity's launchMode
// is set to "singleTop" in the AndroidManifest (important!!!) // 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 // 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 { val intent = Intent(context, MainActivity::class.java).apply {
putExtra(IntentData.openAudioPlayer, true) putExtra(IntentData.openAudioPlayer, true)
putExtra(IntentData.offlinePlayer, offlinePlayer)
addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
} }
return PendingIntentCompat return PendingIntentCompat
.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT, false) .getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT, false)
} }