1

I am writing an Android app that plays music. When the Activity is the foregroud, the media control buttons (Play, Pause, Stop, etc..) are visible. The music should keep playing when the user exits the activity. He can restart the Activity to hit the stop button later if he chooses.

At present, the instance of MediaPlayer is wrapped by a class that is a singleton:

public class MediaPlayerPresenter {
    public static final String TAG = MediaPlayerPresenter.class.getSimpleName();

    MediaPlayer _player;
    PlayerState _state;

    public MediaPlayerPresenter() {
        _state = PlayerState.Idle;
        _player = createMediaPlayer(); // calls new MediaPlayer and hooks up event callbacks
    }

    static public MediaPlayerPresenter getInstance() {
        if (_staticInstance == null) {
            _staticInstance = new MediaPlayerPresenter();
        }
        return _staticInstance;
    }

    // not shown - code that keeps track of MediaPlayer events, state, updating the 
    // view, handling view events, etc...

As it works now, the activity may get destroyed or stopped as a result of the user leaving the application, but the running process (evidently) lives on as music continues to play. When the activity is restarted or brought back to the foreground, the media controls of the view re-associate with the already running MediaPlayer via the singleton instance above.

So far, so good. But I've been reading that background music should be managed by a service. So my questions:

  1. Why is it better to use a Service to maintain the lifetime of the MediaPlayer instance instead of a plain old singleton class? Is it because the lifetime of the singleton running in a process without an Activity or Service is not guaranteed? Or something else?

  2. I'm ok with using a Service in lieu of a singleton. And if I understand correctly, Service and Activities of an application run in the same thread. If that's the case, when my Activity starts back up again, is it ok for it to access the the instance of the MediaPlayer class owned by the Service class directly ? I don't see much value of using a binder interface for the Activity code to to talk to a local Service in the same process.

  3. Any good examples of a background music player using a Service pattern?

selbie
  • 100,020
  • 15
  • 103
  • 173

2 Answers2

4

Background playback is the key here.

The music should keep playing when the user exits the activity.

If that is what you really want then I am afraid a singleton is not an good option. No guarantee if your singleton object will be keep alive once the user exits the activity. Your singleton will be eligible for gc() once the activity is destroyed and when your device runs low on memory, it will be destroyed so will your MediaPlayer lifecycle.
If you are testing this on a device , go to Developer options and check "Don't keep activities ...". Exit the activity and see the outcome.
Personally I will go with a Service, and try to connect back with the service in onResume().
As for the tutorial please refer to this.
Background audio streaming tutorial

Lazy Ninja
  • 22,342
  • 9
  • 83
  • 103
  • Actually, the "Don't keep Activities" option doesn't seem to kill the process either (music keeps playing). However, changing "Background Process Limit" from "Standard limit" to "None" does force the process to die when the Activity goes away. But your answer got me to seek out this link: http://developer.android.com/guide/components/processes-and-threads.html#Lifecycle which indicates that my process is either running as "background" or "empty". Both of which are at the bottom of the priority order for the OS to keep around. Hence, the reason for a Service. – selbie Apr 06 '14 at 04:53
  • Thank you for your update. My guess was that music will stop playing if you change the settings. Probably the MediaPlayer class is running in another thread.I have to look more deeper in it. As for a tutorial, please check my edit. – Lazy Ninja Apr 06 '14 at 05:04
0

Why implementing a Singleton pattern in Java code is (sometimes) considered an anti-pattern in Java world?

http://blogs.msdn.com/b/scottdensmore/archive/2004/05/25/140827.aspx

http://caines.ca/blog/programming/singletons-anti-pattern-or-worst-anti-pattern-ever/

Community
  • 1
  • 1
Andy Librian
  • 911
  • 5
  • 12
  • Thanks Andy. I don't disagree with the purists who state singletons are bad. (I don't agree with everything they say either...) But I'm actually in search of technical reasons the Android platform encourages the use of a Service for background music. That's a lot of programming overhead for controlling the lifetime of a variable. – selbie Apr 06 '14 at 04:20