退出/暂停应用程序时如何暂停背景音乐服务?

时间:2019-02-26 14:00:19

标签: java android android-activity android-service android-service-binding

我想在用户玩游戏时播放背景音乐。音乐在用户启动应用程序时开始,在用户离开应用程序时暂停,在用户返回应用程序时恢复播放。

我尝试使用this method,对其进行了一些编辑:

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();
    }
}

我使用一项活动来播放,暂停和恢复背景音乐。在此,我将省略此问题的不必要的方法/行:

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;
        }
    }
}

但是,当我运行应用程序时,在NullPointerException类中得到了MainActivity(在onCreate方法中,我在代码中对其进行了注释)。

该变量似乎尚未初始化,但是当用户打开应用程序时,我确实需要启动音乐。

我还尝试从backgroundSoundService.start();方法中删除onCreate,因此在调用onStart时开始播放音乐。但是,当我这样做时,会遇到相同的错误。

那么,如何在将backgroundSoundService用于调用其方法之前进行初始化?

1 个答案:

答案 0 :(得分:1)

首先从onCreate中删除此df_dict = dict() for tx in dframe1['tx_id']: df = pd.read_csv(os.path.join('/path/to/csv/', '%s.csv' % tx)) df_dict[tx] = df.groupby('rule_id')['request_id'].value_counts().unstack().fillna(0) 并将其添加到backgroundSoundService.start()方法内部

在执行任何与backgroundSoundService相关的操作之前,您需要检查null

onServiceConnected()

@Override public void onPause() { super.onPause(); if(backgroundSoundService != null){ backgroundSoundService.pause(); } } 的所有外观中添加这种空检查