确保前台服务启动前的状态检查,修复潜在的播放器释放问题
This commit is contained in:
@@ -346,11 +346,14 @@ class LivePlayActivity : AppCompatActivity() {
|
|||||||
|
|
||||||
private fun beginPlayback() {
|
private fun beginPlayback() {
|
||||||
startPlaybackForegroundService()
|
startPlaybackForegroundService()
|
||||||
|
binding.root.post {
|
||||||
|
if (hasReleasedPlayer || isDestroyed) return@post
|
||||||
startPlayAttempt()
|
startPlayAttempt()
|
||||||
resetPreviewForPlayback()
|
resetPreviewForPlayback()
|
||||||
playerClient.prepareToPlay()
|
playerClient.prepareToPlay()
|
||||||
playerClient.play()
|
playerClient.play()
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private fun startPlaybackForegroundService() {
|
private fun startPlaybackForegroundService() {
|
||||||
LivePlayForegroundService.start(this)
|
LivePlayForegroundService.start(this)
|
||||||
|
|||||||
@@ -18,17 +18,15 @@ import com.demo.SellyCloudSDK.R
|
|||||||
*/
|
*/
|
||||||
class LivePlayForegroundService : Service() {
|
class LivePlayForegroundService : Service() {
|
||||||
|
|
||||||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
private var hasStartedForeground = false
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
|
||||||
ServiceCompat.startForeground(
|
override fun onCreate() {
|
||||||
this,
|
super.onCreate()
|
||||||
NOTIFICATION_ID,
|
startForegroundIfNeeded()
|
||||||
buildNotification(),
|
|
||||||
android.content.pm.ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK
|
|
||||||
)
|
|
||||||
} else {
|
|
||||||
startForeground(NOTIFICATION_ID, buildNotification())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
||||||
|
startForegroundIfNeeded()
|
||||||
return START_STICKY
|
return START_STICKY
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -53,6 +51,21 @@ class LivePlayForegroundService : Service() {
|
|||||||
.build()
|
.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() {
|
private fun ensureChannel() {
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||||
val manager = getSystemService(NotificationManager::class.java) ?: return
|
val manager = getSystemService(NotificationManager::class.java) ?: return
|
||||||
@@ -73,13 +86,23 @@ class LivePlayForegroundService : Service() {
|
|||||||
private const val NOTIFICATION_ID = 0x201
|
private const val NOTIFICATION_ID = 0x201
|
||||||
|
|
||||||
fun start(context: Context) {
|
fun start(context: Context) {
|
||||||
val intent = Intent(context, LivePlayForegroundService::class.java)
|
val appContext = context.applicationContext
|
||||||
ContextCompat.startForegroundService(context, intent)
|
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) {
|
fun stop(context: Context) {
|
||||||
val intent = Intent(context, LivePlayForegroundService::class.java)
|
val appContext = context.applicationContext
|
||||||
context.stopService(intent)
|
val intent = Intent(appContext, LivePlayForegroundService::class.java)
|
||||||
|
appContext.stopService(intent)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user