Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -124,6 +123,8 @@ abstract class AbstractPlayerService : LifecycleService() {
}
}

abstract val isOfflinePlayer: Boolean

override fun onCreate() {
super.onCreate()

Expand Down Expand Up @@ -178,7 +179,9 @@ abstract class AbstractPlayerService : LifecycleService() {

nowPlayingNotification = NowPlayingNotification(
this,
player!!
player!!,
backgroundOnly = true,
offlinePlayer = isOfflinePlayer
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,8 @@ class OfflinePlayerActivity : BaseActivity() {

nowPlayingNotification = NowPlayingNotification(
this,
viewModel.player
viewModel.player,
offlinePlayer = true
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>()!!
Expand Down Expand Up @@ -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)
}
Expand Down
Loading