2

I'm writing an music player app. I have the MediaPlayer object in a service. The problem is that I don't know how to update the UI from service. By example I want to update the remaining time from current song but - because the MediaPlayer is on service - I can't set a listener to MediaPlayer object.

I was thinking about sending a broadcast message with current time but if I'll do it in this way, I have to send a message on every second. I suppose this will affect the speed of application + battery life.

Another solution can be to create a public static variable in Service and access it from activity but I read that this is not a good option.

What do you think? Do you know any - better - way to update the UI from service?

Thank you.

Ungureanu Liviu
  • 4,034
  • 4
  • 37
  • 42

2 Answers2

3

You can do this by binding your service to activity .Perform this using AIDL in android.

Here is a very good example of AIDL

What you actually need to do is create your MediaPlayer in your SERVICE, create a class that extends ServiceConnection and in your .aidl file make some interface methods like this :

interface IPlayerService {
    // You can pass values in, out, or inout. 
    // Primitive datatypes (such as int, boolean, etc.) can only be passed in.

    void play();
     void pause();
     void stop();
     boolean isPlaying();
     int maxDuration();
     int getCurrentPosition();
     void changePosition(int a);

     void seekComplete();

     boolean isMusicCompleted();

} 

Define these methods in your service class by creating a new stub in onBind(Intent intent) method of the service class.

Now from your mainActvity interact with service to update your UI .

How to perform these things is given by an example in the above link of AIDL & bind music app to service.

Hope this will help you to solve your issue.

Dinesh Sharma
  • 11,533
  • 7
  • 42
  • 60
  • Thanks! I am also search this. Its helpful to me. – Newts Feb 08 '12 at 08:07
  • Thanks for your response. If I will create my media player object in activity, the music will stop when the activity get destroyed. To avoid this, I have to create the MediaPlayer in service and from service I have to "push" data (current track name, remaining seconds) to activity. – Ungureanu Liviu Feb 08 '12 at 19:57
  • SORRY for that, yes you are right , you have create MediaPlayer in service and interact with service from your activity using the methods in your .aidl file . – Dinesh Sharma Feb 09 '12 at 05:12
  • 1
    No need to use aidl if the service is running in the same process as the client (and if you have no intention to export the service functionality to other applications). A local service is much simpler. –  Feb 09 '12 at 06:50
1

I haven’t worked with the media player, but I assume that you want to start the service from your activity (using startService) to start playing music, and then you want to bind to the service (using bindService) to be able to register listeners that will deliver information back from the service (the service will wrap the callbacks from the media player) to the activity and offer the possibility to change track etc. while the activity is visible.

So, you need to implement your service as both a started service (to be able to play music while you application is in the background) and a service that it is possible to bind to (to be able to control it when your application is in the foreground). Assuming that you service is running in the same process as your client activity it is very simple to implement this. And, I would also recommend you service to be running in the foreground to avoid it being killed by the system.

Started service: http://developer.android.com/guide/topics/fundamentals/services.html#ExtendingIntentService

Local service that your client activity should bind to when it is displayed: http://developer.android.com/guide/topics/fundamentals/bound-services.html#Binder

  • You're right. Thanks for your answer. I will use local service to attach a listener to MediaPlayer which is running in service. – Ungureanu Liviu Feb 08 '12 at 20:01