From d8fd9e8bc95f178c75f9f5b02ea5a40674df46e4 Mon Sep 17 00:00:00 2001 From: shou Date: Wed, 4 Feb 2026 10:05:31 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A1=AE=E4=BF=9D=E5=89=8D=E5=8F=B0=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=E5=90=AF=E5=8A=A8=E5=89=8D=E7=9A=84=E7=8A=B6=E6=80=81?= =?UTF-8?q?=E6=A3=80=E6=9F=A5=EF=BC=8C=E4=BF=AE=E5=A4=8D=E6=BD=9C=E5=9C=A8?= =?UTF-8?q?=E7=9A=84=E6=92=AD=E6=94=BE=E5=99=A8=E9=87=8A=E6=94=BE=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SellyCloudSDK/live/LivePlayActivity.kt | 11 ++-- .../live/LivePlayForegroundService.kt | 51 ++++++++++++++----- 2 files changed, 44 insertions(+), 18 deletions(-) diff --git a/example/src/main/java/com/demo/SellyCloudSDK/live/LivePlayActivity.kt b/example/src/main/java/com/demo/SellyCloudSDK/live/LivePlayActivity.kt index c01094b..88821e3 100644 --- a/example/src/main/java/com/demo/SellyCloudSDK/live/LivePlayActivity.kt +++ b/example/src/main/java/com/demo/SellyCloudSDK/live/LivePlayActivity.kt @@ -346,10 +346,13 @@ class LivePlayActivity : AppCompatActivity() { private fun beginPlayback() { startPlaybackForegroundService() - startPlayAttempt() - resetPreviewForPlayback() - playerClient.prepareToPlay() - playerClient.play() + binding.root.post { + if (hasReleasedPlayer || isDestroyed) return@post + startPlayAttempt() + resetPreviewForPlayback() + playerClient.prepareToPlay() + playerClient.play() + } } private fun startPlaybackForegroundService() { diff --git a/example/src/main/java/com/demo/SellyCloudSDK/live/LivePlayForegroundService.kt b/example/src/main/java/com/demo/SellyCloudSDK/live/LivePlayForegroundService.kt index 53a64ac..290260f 100644 --- a/example/src/main/java/com/demo/SellyCloudSDK/live/LivePlayForegroundService.kt +++ b/example/src/main/java/com/demo/SellyCloudSDK/live/LivePlayForegroundService.kt @@ -18,17 +18,15 @@ import com.demo.SellyCloudSDK.R */ class LivePlayForegroundService : Service() { + private var hasStartedForeground = false + + override fun onCreate() { + super.onCreate() + startForegroundIfNeeded() + } + override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { - ServiceCompat.startForeground( - this, - NOTIFICATION_ID, - buildNotification(), - android.content.pm.ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK - ) - } else { - startForeground(NOTIFICATION_ID, buildNotification()) - } + startForegroundIfNeeded() return START_STICKY } @@ -53,6 +51,21 @@ class LivePlayForegroundService : Service() { .build() } + private fun startForegroundIfNeeded() { + if (hasStartedForeground) return + hasStartedForeground = true + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + ServiceCompat.startForeground( + this, + NOTIFICATION_ID, + buildNotification(), + android.content.pm.ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK + ) + } else { + startForeground(NOTIFICATION_ID, buildNotification()) + } + } + private fun ensureChannel() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { val manager = getSystemService(NotificationManager::class.java) ?: return @@ -73,13 +86,23 @@ class LivePlayForegroundService : Service() { private const val NOTIFICATION_ID = 0x201 fun start(context: Context) { - val intent = Intent(context, LivePlayForegroundService::class.java) - ContextCompat.startForegroundService(context, intent) + val appContext = context.applicationContext + val intent = Intent(appContext, LivePlayForegroundService::class.java) + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + try { + appContext.startService(intent) + } catch (_: IllegalStateException) { + ContextCompat.startForegroundService(appContext, intent) + } + } else { + appContext.startService(intent) + } } fun stop(context: Context) { - val intent = Intent(context, LivePlayForegroundService::class.java) - context.stopService(intent) + val appContext = context.applicationContext + val intent = Intent(appContext, LivePlayForegroundService::class.java) + appContext.stopService(intent) } } }