M.Baraka
M.Baraka

Reputation: 755

Android Media Player using service

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 ?

Upvotes: 4

Views: 8150

Answers (2)

Pratik Dasa
Pratik Dasa

Reputation: 7439

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

Upvotes: 1

Tofeeq Ahmad
Tofeeq Ahmad

Reputation: 11975

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

Upvotes: 3

Related Questions