I have a service that when it finishs running tasks it plays a notification with sound. As it ends, my calling application, if it is on top calls to stopservice. The problem is that notificationmanager returns before the sound ends, so the application stops the service before the sound has finished (if it is long). Is there a way I can detect when the sound has finish in order to not let the service stops before the sound ends?.
I have been looking into soundmanager and other threads without luck. Also, A workaround is to put a Thread.sleep(ms), but if the notificatio sound is long, it could be stopped anyway, and I would like responsitivity from my app just after service finishs.
A possible solution would be to calculate the length of the mp3 file but I dont know how to do it….
I have a service that when it finishs running tasks it plays a notification
Share
Assuming you are using a
MediaPlayerfor playback, you can get the duration withMediaPlayer.getDuration().Please note, that this won’t work with some VBR mp3. so you might want to save the mp3 with a constant bitrate or run a tool like vbrfix before putting them in your app.
The duration is returned in milliseconds, so you can simply pass this value to
SystemClock.sleep()/edit:
Okay, if you have the file saved as an asset, you can use this code: