mirror of
https://github.com/KRTirtho/spotube.git
synced 2026-11-04 10:13:59 +00:00
fix(android): simplify service start logic for PlaybackService to avoid crashes on background playback
This commit is contained in:
parent
9d5854023b
commit
0b21eff4c7
@ -18,7 +18,6 @@
|
|||||||
package org.spotube.spotube
|
package org.spotube.spotube
|
||||||
|
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.os.Build
|
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import androidx.activity.ComponentActivity
|
import androidx.activity.ComponentActivity
|
||||||
import androidx.activity.compose.setContent
|
import androidx.activity.compose.setContent
|
||||||
@ -37,13 +36,8 @@ class MainActivity : ComponentActivity() {
|
|||||||
FileKit.init(this)
|
FileKit.init(this)
|
||||||
NewPipeDownloader.init(Paths())
|
NewPipeDownloader.init(Paths())
|
||||||
|
|
||||||
// Start PlaybackService from Activity context (allowed on Android 12+)
|
// Plain start from the foreground; Media3 promotes PlaybackService itself once playback starts
|
||||||
val serviceIntent = Intent(this, PlaybackService::class.java)
|
startService(Intent(this, PlaybackService::class.java))
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
||||||
startForegroundService(serviceIntent)
|
|
||||||
} else {
|
|
||||||
startService(serviceIntent)
|
|
||||||
}
|
|
||||||
|
|
||||||
intent?.dataString?.let(ExternalUriHandler::onNewUri)
|
intent?.dataString?.let(ExternalUriHandler::onNewUri)
|
||||||
setContent {
|
setContent {
|
||||||
|
|||||||
@ -19,7 +19,6 @@ package org.spotube.spotube.core.audioplayer
|
|||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.os.Build
|
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import androidx.media3.common.AudioAttributes
|
import androidx.media3.common.AudioAttributes
|
||||||
import androidx.media3.common.C
|
import androidx.media3.common.C
|
||||||
@ -52,17 +51,14 @@ actual class AudioPlayer actual constructor(context: Any) : AudioPlayerInterface
|
|||||||
private val appContext: Context = (context as Context).applicationContext
|
private val appContext: Context = (context as Context).applicationContext
|
||||||
|
|
||||||
private fun ensureServiceStarted() {
|
private fun ensureServiceStarted() {
|
||||||
// On Android 12+ starting a foreground service from the background throws
|
// A plain start, never startForegroundService: that obliges the service to call
|
||||||
// (ForegroundServiceStartNotAllowedException) — e.g. when a jam session or
|
// startForeground() within seconds of *every* call, and once Media3 has demoted it
|
||||||
// remote control applies playback while the app is backgrounded. Never let
|
// on pause nothing does, so the app is killed (ForegroundServiceDidNotStartInTime).
|
||||||
// that crash the app; playback itself runs in-process without the service.
|
// Media3's MediaSessionService promotes itself to the foreground when playback starts.
|
||||||
|
// Starting from the background (jam sync, remote control) throws; playback still runs
|
||||||
|
// in-process without the service, so never let that crash the app.
|
||||||
try {
|
try {
|
||||||
val intent = Intent(appContext, PlaybackService::class.java)
|
appContext.startService(Intent(appContext, PlaybackService::class.java))
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
||||||
appContext.startForegroundService(intent)
|
|
||||||
} else {
|
|
||||||
appContext.startService(intent)
|
|
||||||
}
|
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
Log.w("AudioPlayer", "Failed to start playback service", e)
|
Log.w("AudioPlayer", "Failed to start playback service", e)
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user