I have a service that creates, plays and handles a mediaplayer (just audio), yet I have a seekbar in the main activity that I want to, naturally, show the progress of the audio file and allow the user to seek to various positions.
What I'm having a hell of a time figuring out is: what would be the best or proper way to connect the seekbar in the UI to the mediaplayer in the service?
Here is how I would do this:
Bind to service that plays audio. The interface that server returns in onBind
should have getCurrentPos()
and getDuration()
functions.
In your Activity's onCreate
bind to service
onResume
use Handler
's postDelayed
function to start updates.onPause
stop all callbacks posted via postDelayed
function.postDelayed
run boundService.getCurrentPos()
and boundService.getDuration()
and update the seekbar appropriately.To summarize, you should use service binding and handler for recurring updates.
Handler
for periodic updates read this question: Repeat a task with a time delay?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With