Reputation: 41
I have read many answers to the problem of receiving the same massage repeatedly by a Broadcast receiver. The solutions is are mostly saying register the receiver in OnResume an unregister in OnPause. This is not possible in my code. I have UI (media player of sorts) and a bound Musicservive.. all is working well except for this multiple message received problem. When a media file finishes the Musicservice sends a message "Cue Completed" the receiver is getting the message. I then need to determine if the next media needs to directly play or wait for a button press. I can get it to play the next without any problem, However, if i want it not to play the next piece of music just keeps getting the same messaage and forwards till reaching the end . some code
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
}
}
the above is in mymusicservice.
The following is from my UI activity 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))
When I run the code this is an example from the logcat
2019-03-04 16:53:43.111 1194-1235/? I/ActivityManager: Displayed com.ktdevelopement.nigelmccullagh.qsoundkt/com.ktdevelopment.nigelmccullagh.qsoundkt.FullScreenActivity: +225ms 2019-03-04 16:53:46.369 29716-29716/? I/FullScreenActivity: Service got intent with action: Media Player Status and status mp IS_PLAYING 2019-03-04 16:53:48.876 29716-29716/? I/FullScreenActivity: Service got intent with action: Media Player Status and status mp Fading Out 2019-03-04 16:53:51.791 29716-29716/? I/FullScreenActivity: Service got intent with action: Media Player Status and status mp cue_completed 2019-03-04 16:53:52.238 29716-29716/? I/FullScreenActivity: Service got intent with action: Media Player Status and status mp IS_PLAYING 2019-03-04 16:53:54.350 29716-29716/? I/FullScreenActivity: Service got intent with action: Media Player Status and status mp Fading Out 2019-03-04 16:53:54.490 29716-29716/? I/FullScreenActivity: Service got intent with action: Media Player Status and status mp Fading Out 2019-03-04 16:53:55.308 29716-29716/? I/FullScreenActivity: Service got intent with action: Media Player Status and status mp Fading Out 2019-03-04 16:53:55.503 29716-29716/? I/FullScreenActivity: Service got intent with action: Media Player Status and status mp Fading Out 2019-03-04 16:53:55.986 29716-29716/? I/FullScreenActivity: Service got intent with action: Media Player Status and status mp cue_completed 2019-03-04 16:53:56.059 29716-29716/? I/FullScreenActivity: Service got intent with action: Media Player Status and status mp cue_completed 2019-03-04 16:53:56.212 29716-29716/? I/FullScreenActivity: Service got intent with action: Media Player Status and status mp cue_completed 2019-03-04 16:53:56.245 29716-29716/? I/FullScreenActivity: Service got intent with action: Media Player Status and status mp cue_completed 2019-03-04 16:53:56.278 29716-29716/? I/FullScreenActivity: Service got intent with action: Media Player Status and status mp Fading Out 2019-03-04 16:53:56.279 29716-29716/? I/FullScreenActivity: Service got intent with action: Media Player Status and status mp IS_PLAYING 2019-03-04 16:53:56.281 29716-29716/? I/FullScreenActivity: Service got intent with action: Media Player Status and status mp cue_completed 2019-03-04 16:53:56.477 29716-29716/? I/FullScreenActivity: Service got intent with action: Media Player Status and status mp Stopped
Its frustrating me the rest of my app is working fine, but I cannot for the life me sort this out. I have been working on it for 3 days.
any help would be appreciated.
Upvotes: 0
Views: 160
Reputation: 41
You were absolutely correct. I was sending the same broadcast more than once. I discovered it inside a runnable designed to fade out the music.
Upvotes: 0