【问题标题】:How to pause background music service when you exit/pause the application?退出/暂停应用程序时如何暂停背景音乐服务?
【发布时间】:2019-07-20 02:20:25
【问题描述】:

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

我尝试使用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;
        }
    }
}

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

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

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

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

【问题讨论】:

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


    【解决方案1】:

    首先从 onCreate 中删除这个 backgroundSoundService.start() 并将其添加到 onServiceConnected() 方法中

    在执行任何与 backgroundSoundService 相关的事情之前,您需要检查 null,如下所示

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

    backgroundSoundService的所有外观中添加这种空检查

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多