4

I created a MediaPlayer class to play mp3 files. Everything was fine, then my player stopped playing while it was in background and I found out that my problem was I did not create it in a service, so I started to read about services to learn how to create one and use it for my player.

My question is, what is the best way for me to communicate between the service and my application? Should I use the message or send intents or bind it?

I should also create a UI in notification area, also to show the progress buffering. Do I create a listener from the service side to the activity or there is better way?

I used these examples to learn but I did not learn how to use the onBind method yet:

Thanks in advance.

EDIT : I need to sometimes ask the player to send me the track details which is playing, and the player to tell me the buffer updates so i can update seek bar. Do I keep sending intents to players or should I use IBinder ?

Community
  • 1
  • 1
M.Baraka
  • 725
  • 1
  • 10
  • 24

2 Answers2

3

Bind your Service to Activity (Say your application), When ever you open it to control MediaPlayer. See Binding Service Tutorial Series. Use foreground service so that its priority always be high and it get killed as a late as possible in memory low sitiuaton

Tofeeq Ahmad
  • 11,935
  • 4
  • 61
  • 87
1

To communicate between service and Activity, I prefer Broadcast Receiver. Send Broadcast from Service and receive it in Activity.

Pratik Dasa
  • 7,439
  • 4
  • 30
  • 44
  • so i keep sending in reciever messages like Play ,Pause ..etc and use for example switch case to handle every thing ? @pratik – M.Baraka Aug 24 '13 at 13:01
  • What exactly you want to send from service to activity? I mean which kind of data? – Pratik Dasa Aug 24 '13 at 13:03
  • my program is 1 activiy mainly and many fragments ,but i meant should i send to the playerService commands like Play,Pause..etc ?also will it be good if i create a listener to keep track of the buffer update ?@pratik – M.Baraka Aug 24 '13 at 22:51
  • Yes you can send player commands like Play, pause etc to Avtivity use sendBroadcast() method to broadcast it, you can find it on google, there is many tutorial, and you can create listener as well for buffer update. Use same thing in buffer update also. – Pratik Dasa Aug 26 '13 at 08:59