0

I'm trying to add background music to my game and I thought I could persist it across activities by starting the service and then just connecting and binding to it in different activities in order to control it. But my music stops playing when I try to establish a connection from my second activity. I'm very new to working with services, so I apologize for any simple mistakes.

From my MusicService class (extends Service)

private final IBinder mBinder = new ServiceBinder();
MediaPlayer mPlayer;
private int length = 0;

public static boolean STOP_ON_DESTROY = true;

// Binder subclass. Allows access from clients to the server
public class ServiceBinder extends Binder {
    MusicService getService(){
        return MusicService.this;
    }
}

public MusicService() {}

@Override
public IBinder onBind(Intent intent) {
    return mBinder;
}

@Override
public void onCreate(){
    super.onCreate();
}

@Override
public int onStartCommand (Intent intent, int flags, int startId)
{
    return START_STICKY;
}

public boolean isPlaying(){
    if(mPlayer != null){
        return mPlayer.isPlaying();
    }
    return false;
}

public void stopMusic()
{
    if(mPlayer != null){
        if(mPlayer.isPlaying()) {
            mPlayer.stop();
            mPlayer.release();
            mPlayer = null;
        }

        System.out.println("stopMusic service fn");
    }
}

This is the code I call in both my Main and secondary activities in order to interact with the service. The music stops during the connectToMusicService function in the secondary activity. The Main activity works great.

onCreate(){....
startMusicService();
    MusicService.STOP_ON_DESTROY = true;
}

private void startMusicService() {
    Intent musicIntent = new Intent();
    musicIntent.setClass(getApplicationContext(), MusicService.class);
    startService(musicIntent);
}

@Override
public void onStart(){
    super.onStart();
    // establish connection for binding to the service
    connectToMusicService();
    // bind to the service
    bindToMusicService();
}

private void bindToMusicService() {
    Intent intent = new Intent(this, MusicService.class);
    bindService(intent, myServiceConnection, Context.BIND_AUTO_CREATE);
    MusicService.STOP_ON_DESTROY = true;
}

private void connectToMusicService() {
    myServiceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            MusicService.ServiceBinder binder = (MusicService.ServiceBinder) service;
            mService = binder.getService();
            if(!mService.isPlaying())
                mService.startMusic();
            isServiceBound = true;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            System.out.println("Service disconnected from main");
            isServiceBound = false;
        }
    };
}

The only thing I do during my Main activity's onStop is

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

    if(mService != null) {
        if (MusicService.STOP_ON_DESTROY) {
            mService.stopMusic();
        }
    }
}

UPDATE: I got it working. My issue wasn't with service binding at all. It was with static STOP_ON_DESTROY variable I was using to manage whether the music should stop when leaving an activity. I cleaned that up and all is good now. Thanks!

4

1 回答 1

0

First of all, do you need to bind at all? Or could starting the service be enough? Started services run until you stop them (except if killed by the system when resources are scarce). I am not sure there's any point binding from each of your activities. Btw if your service should run and play music also when your activities are closed, consider making it a foreground service.

于 2015-03-15T15:45:53.550 回答