如何解决Android打ze模式会终止前卫媒体播放器服务
我一直在研究如何在后台(在线广播)中保持连续运行的音频播放活跃。最后,我为其提供了前台服务,并且可以在大多数手机上使用,但不能在Samsung Android P及更高版本上使用...(如“前台服务限制”一节中所示:https://proandroiddev.com/android-foreground-service-restrictions-d3baa93b2f70) 我听说有一个名为ExoPlayer的高级音频播放工具。这个lib可以帮我吗?
我已经尝试过以下解决方案:
-
每2秒
- ping google.com
- 请求电池优化忽略
- 使用以下方法为mediplayer设置唤醒锁:setWakeMode(context,PowerManager.PARTIAL_WAKE_LOCK)(仍在使用)
启动服务:
viewModel.isMusicControlServiceNeedToStart.observe(this,Observer {
if (it) {
val intent = Intent(this,MusicControlForegroundServiceImpl::class.java).apply { action = ACTION_SHOW_MUSIC_CONTROL }
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) startForegroundService(intent) else startService(intent)
} else {
stopService(Intent(this,MusicControlForegroundServiceImpl::class.java))
}
})
服务本身:
class MusicControlForegroundServiceImpl : Service(),KoinComponent {
private val notificationManager: NotificationManager by inject()
private val radioManager: RadioManager by inject()
private val context: Context by inject()
private val preferences: Preferences by inject()
override fun onStartCommand(intent: Intent?,flags: Int,startId: Int): Int {
if (intent != null && intent.action == ACTION_SHOW_MUSIC_CONTROL) {
val lastSelectedRadio = preferences.getJSON(Globals.LAST_SELECTED_RADIO_KEY,Radio::class.java)
?: return START_NOT_STICKY
val notification = notificationManager.createMediaControlNotificationIfNeeded(context,lastSelectedRadio)
startForeground(1,notification)
}
return START_NOT_STICKY
}
override fun onTaskRemoved(rootIntent: Intent?) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) startForegroundService(rootIntent) else startService(rootIntent)
super.onTaskRemoved(rootIntent)
}
override fun onDestroy() {
if (!notificationManager.musicControlServiceRestart) radioManager.release()
synchronized(MUSIC_CONTROL_SERVICE_LOCK) { notificationManager.musicControlServiceRestart = false }
synchronized(MEDIA_PLAYER_LOCK) { radioManager.lastPlayedMediaUrl = null }
stopForeground(true)
super.onDestroy()
}
override fun onBind(intent: Intent?): IBinder? = null
}
通知创建:
override fun createMediaControlNotificationIfNeeded(context: Context,selectedRadio: Radio): Notification {
val resultIntent = Intent(context,RadioDetailActivity::class.java)
val resultPendingIntent: PendingIntent? = TaskStackBuilder.create(context).run {
addNextIntentWithParentStack(resultIntent)
getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT)
}
val playIntent = Intent(context,NotificationReceiver::class.java).apply {
putExtra(MusicState::class.java.name,MusicState.PLAY)
}
val pauseIntent = Intent(context,MusicState.PAUSE)
}
val notificationManager = NotificationManagerCompat.from(context)
@Suppress("DEPRECATION") val builder = NotificationCompat.Builder(context,CHANNEL_ID)
.setSmallIcon(R.drawable.ic_notification_icon)
.setContentTitle(selectedRadio.name)
.setDefaults(0)
.setOngoing(true)
.setNotificationSilent()
.addAction(
R.drawable.ic_notification_pause,context.getString(R.string.pause),PendingIntent.getBroadcast(context,1,pauseIntent,0)
)
.addAction(
R.drawable.ic_notification_play,context.getString(R.string.play),2,playIntent,0)
)
.setStyle(
androidx.media.app.NotificationCompat.MediaStyle().setMediaSession(
MediaSessionCompat(
context,RadioDetailActivity::class.java.name
).sessionToken
)
)
.setContentIntent(resultPendingIntent)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(
CHANNEL_ID,context.getString(R.string.app_name),NotificationManager.IMPORTANCE_LOW
)
notificationManager.createNotificationChannel(channel)
builder.setChannelId(CHANNEL_ID)
}
return builder.build()
}
如果您需要任何其他资源,请让我知道并提供帮助!我已经为这个问题苦苦挣扎了好几个星期了..:(
更新 现在,我每隔2分钟会发出媒体控制通知以更新以前的内容,因此该应用程序在受影响的手机上可以像30分钟一样存活,但仍然无法正常工作...
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。