How to communicate between music playback service and user interface stream

I have a list of music titles in a ListView.

I can click on each item to play music through MediaPlayer in the service.

Now I want to implement 2 Features:

  • Music ProgressBar showing the current position in the song
  • The service will continue playing when Song ends with the next song in the list.

So how can I update the user interface from the background service? (I know that there are several solutions on Stackoverflow, but it seems to me that I'm a bit overloaded to send a broadcast every second from the ie Service). Should I use binding? What is the advantage of binding? Now I just start the service using Intent (startService (intent)), which contains the path of the song.

What about the second question? How can i do this?

+3
source share
2 answers

I assume that you built the service yourself. This way you know how it is built and how to access the mediaPlayer link. What you need to do is turn your service into a related service. You may need to start your service through startService(otherwise, the related service will not sustain your activity), but later you will have to get attached to it from within your activity.

, IBinder ( ) mediaPlayer IBinder. , - ( !).

, , ​​.

+2

, /. -- - github-

( , / ). ,

  • → - Messenger
  • → / - BroadcastReceiver

github ( 500 , ), ...

Btw. , .

0

All Articles