diff --git a/app/src/main/java/com/github/libretube/services/AbstractPlayerService.kt b/app/src/main/java/com/github/libretube/services/AbstractPlayerService.kt index 92a71da2c..036fb54a3 100644 --- a/app/src/main/java/com/github/libretube/services/AbstractPlayerService.kt +++ b/app/src/main/java/com/github/libretube/services/AbstractPlayerService.kt @@ -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 ) } diff --git a/app/src/main/java/com/github/libretube/services/OfflinePlayerService.kt b/app/src/main/java/com/github/libretube/services/OfflinePlayerService.kt index ca209e30d..9c61ac355 100644 --- a/app/src/main/java/com/github/libretube/services/OfflinePlayerService.kt +++ b/app/src/main/java/com/github/libretube/services/OfflinePlayerService.kt @@ -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 diff --git a/app/src/main/java/com/github/libretube/services/OnlinePlayerService.kt b/app/src/main/java/com/github/libretube/services/OnlinePlayerService.kt index f08613650..e7f2f2b62 100644 --- a/app/src/main/java/com/github/libretube/services/OnlinePlayerService.kt +++ b/app/src/main/java/com/github/libretube/services/OnlinePlayerService.kt @@ -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() + // SponsorBlock Segment data + private var sponsorBlockSegments = listOf() 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) { diff --git a/app/src/main/java/com/github/libretube/ui/activities/MainActivity.kt b/app/src/main/java/com/github/libretube/ui/activities/MainActivity.kt index 6d0a196f2..f20dbffc4 100644 --- a/app/src/main/java/com/github/libretube/ui/activities/MainActivity.kt +++ b/app/src/main/java/com/github/libretube/ui/activities/MainActivity.kt @@ -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 } diff --git a/app/src/main/java/com/github/libretube/ui/activities/OfflinePlayerActivity.kt b/app/src/main/java/com/github/libretube/ui/activities/OfflinePlayerActivity.kt index 4e51f5cc7..5c27fc678 100644 --- a/app/src/main/java/com/github/libretube/ui/activities/OfflinePlayerActivity.kt +++ b/app/src/main/java/com/github/libretube/ui/activities/OfflinePlayerActivity.kt @@ -219,7 +219,8 @@ class OfflinePlayerActivity : BaseActivity() { nowPlayingNotification = NowPlayingNotification( this, - viewModel.player + viewModel.player, + offlinePlayer = true ) } diff --git a/app/src/main/java/com/github/libretube/util/NowPlayingNotification.kt b/app/src/main/java/com/github/libretube/util/NowPlayingNotification.kt index b1b15edd8..4b69ebc6b 100644 --- a/app/src/main/java/com/github/libretube/util/NowPlayingNotification.kt +++ b/app/src/main/java/com/github/libretube/util/NowPlayingNotification.kt @@ -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()!! @@ -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) }