我已经阅读了许多有关广播接收机重复接收相同消息的问题的答案。解决方案主要是说在OnResume中注册接收者,在OnPause中注销。这在我的代码中是不可能的。我有UI(各种媒体播放器)和绑定的Musicservive ..一切正常,除了收到多条消息的问题。媒体文件完成后,Musicservice将发送一条消息“提示已完成”,接收方将收到该消息。然后,我需要确定下一个媒体是否需要直接播放或等待按下按钮。我可以让它播放下一首歌曲而没有任何问题,但是,如果我希望它不播放下一首音乐,则只会不断收到相同的信息,然后继续播放直至结束。一些代码
mp.setOnCompletionListener {
if (loop > 0 && loopCount<loop+1) {
Toast.makeText(this, "$loopCount of $loop loops", Toast.LENGTH_LONG).show()
getCurrentsoundPosition()
runCue(position)
Log.i("loopcount", "$loopCount ")
loopCount++
}else {
if(trigger=="Play-Next" || trigger=="Fade Play-Next"){
mpStatus = "cue_completed"
sendMediaPlayerStatus()}else
{mpStatus="Stopped"
sendMediaPlayerStatus()
}
Log.i("status after completion", mpStatus)
loopCount=0
}
}
以上内容在mymusicservice中。
以下是我的UI活动OnCreate中的
val localBroadcastManager = LocalBroadcastManager.getInstance(this)
localBroadcastManager.registerReceiver(object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
// try {
Log.i("FullScreenActivity ", "Service got intent with action: ${intent.action} and status mp ${intent.getStringExtra("status")}")
mpStatus = intent.getStringExtra("status")
when (mpStatus) {
"cue_completed" -> {
goToNext()
tvStatus.clearAnimation()
if ( AutoPlayNext == true) {
btnPlayCue.callOnClick()
}else{
myMusicService?.stopCue()
}
}
"PAUSED" -> tvStatus.text = "PAUSED"
"IS_PLAYING" -> {tvStatus.text = "PLAYING"
updateSeekbar()}
"Fading Out" -> tvStatus.text = "FADING OUT"
}
}
}, IntentFilter(BoundMusicService.ACTION_MEDIAPLAYER_STATUS))
运行代码时,这是logcat中的一个例子
2019-03-04 16:53:43.111 1194-1235 /? I / ActivityManager:显示的com.ktdevelopment.nigelmccullagh.qsoundkt / com.ktdevelopment.nigelmccullagh.qsoundkt.FullScreenActivity:+ 225ms 2019-03-04 16:53:46.369 29716-29716 /? I / FullScreenActivity:服务已采取行动:Media Player状态和状态mp IS_PLAYING 2019-03-04 16:53:48.876 29716-29716 /? I / FullScreenActivity:服务已采取行动:Media Player状态和状态mp淡出 2019-03-04 16:53:51.791 29716-29716 /? I / FullScreenActivity:服务已采取以下措施:Media Player状态和状态mp cue_completed 2019-03-04 16:53:52.238 29716-29716 /? I / FullScreenActivity:服务已采取行动:Media Player状态和状态mp IS_PLAYING 2019-03-04 16:53:54.350 29716-29716 /? I / FullScreenActivity:服务已采取行动:Media Player状态和状态mp淡出 2019-03-04 16:53:54.490 29716-29716 /? I / FullScreenActivity:服务已采取行动:Media Player状态和状态mp淡出 2019-03-04 16:53:55.308 29716-29716 /? I / FullScreenActivity:服务已采取行动:Media Player状态和状态mp淡出 2019-03-04 16:53:55.503 29716-29716 /? I / FullScreenActivity:服务已采取行动:Media Player状态和状态mp淡出 2019-03-04 16:53:55.986 29716-29716 /? I / FullScreenActivity:服务已采取以下措施:Media Player状态和状态mp cue_completed 2019-03-04 16:53:56.059 29716-29716 /? I / FullScreenActivity:服务已采取以下措施:Media Player状态和状态mp cue_completed 2019-03-04 16:53:56.212 29716-29716 /? I / FullScreenActivity:服务已采取以下措施:Media Player状态和状态mp cue_completed 2019-03-04 16:53:56.245 29716-29716 /? I / FullScreenActivity:服务已采取以下措施:Media Player状态和状态mp cue_completed 2019-03-04 16:53:56.278 29716-29716 /? I / FullScreenActivity:服务已采取行动:Media Player状态和状态mp淡出 2019-03-04 16:53:56.279 29716-29716 /? I / FullScreenActivity:服务已采取行动:Media Player状态和状态mp IS_PLAYING 2019-03-04 16:53:56.281 29716-29716 /? I / FullScreenActivity:服务已采取以下措施:Media Player状态和状态mp cue_completed 2019-03-04 16:53:56.477 29716-29716 /? I / FullScreenActivity:服务已采取行动:Media Player状态和状态mp已停止
这让我感到沮丧,我的应用程序其余部分都可以正常工作,但是我终生无法解决这个问题。我已经做了3天了。
任何帮助将不胜感激。
答案 0 :(得分:0)
您绝对正确。我多次发送相同的广播。我在一个旨在消除音乐的可运行物体中发现了它。