Sacha
Sacha

Reputation: 893

How to pause background music service when you exit/pause the application?

I want to have background music playing while the user is playing a game. The music starts when the user starts the application, pauses when they leave it, and it resumes when they go back to the application.

I tried using this method, I edited it a bit:

public class MainActivity extends Activity {

    private boolean bounded;
    private BackgroundSoundService backgroundSoundService;

    ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceDisconnected( ComponentName name ) {
            bounded = false;
            backgroundSoundService = null;
        }

        @Override
        public void onServiceConnected( ComponentName name, IBinder service ) {
            bounded = true;
            BackgroundSoundService.LocalBinder localBinder = (BackgroundSoundService.LocalBinder) service;
            backgroundSoundService = localBinder.getServiceInstance();
        }
    };

    @Override
    public void onCreate( Bundle savedInstanceState ) {
        super.onCreate(savedInstanceState);
        // (code that's not necessary)

        backgroundSoundService.start(); // this is where the error is thrown
    }

    @Override
    public void onPause() {
        super.onPause();

        backgroundSoundService.pause();
    }

    @Override
    public void onResume() {
        super.onResume();

        backgroundSoundService.resume();
    }

    @Override
    public void onStop() {
        super.onStop();

        backgroundSoundService.pause();
    }

    @Override
    public void onStart() {
        super.onStart();

        Intent intent = new Intent(this, BackgroundSoundService.class);
        bindService(intent, connection, BIND_AUTO_CREATE);

        backgroundSoundService.start();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        backgroundSoundService.destroy();
    }
}

I use an activity to play, pause and resume background music. I'll leave out the unecessary methods/lines for this question here:

public class BackgroundSoundService extends Service {
    private static final String TAG = null;
    public IBinder binder = new LocalBinder();

    public IBinder onBind( Intent arg0 ) {
        return binder;
    }

    public IBinder onUnBind( Intent arg0 ) {
        return null;
    }

    public class LocalBinder extends Binder {
        public BackgroundSoundService getServiceInstance() {
            return BackgroundSoundService.this;
        }
    }
}

However, when I run the application I get a NullPointerException in the MainActivity class (in the onCreate method, I commented it in the code).

The variable doesn't seem to be initialized yet, but I do need to start the music when the user opens the application.

I also tried removing the backgroundSoundService.start(); from the onCreate method, so the music would start when onStart is called. However, when I do that, I get the same error.

So, how can I initialize backgroundSoundService before it is used to call its methods?

Upvotes: 1

Views: 369

Answers (1)

PushpikaWan
PushpikaWan

Reputation: 2545

first of all remove this backgroundSoundService.start() from onCreate and add it inside onServiceConnected() method

u need to check null before doing any backgroundSoundService related stuffs like below

 @Override
    public void onPause() {
        super.onPause();
        if(backgroundSoundService != null){
           backgroundSoundService.pause();
        }
    }

add this kind of null check in all appearance of backgroundSoundService

Upvotes: 1

Related Questions