簡體   English   中英

退出/暫停應用程序時如何暫停背景音樂服務?

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

我想在用戶玩游戲時播放背景音樂。 音樂在用戶啟動應用程序時開始,離開應用程序時暫停,並在用戶返回應用程序時繼續播放。

我嘗試使用這種方法,我對其進行了一些編輯:

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方法中,我在代碼中對其進行了注釋)。

該變量似乎尚未初始化,但我確實需要在用戶打開應用程序時啟動音樂。

我也嘗試刪除backgroundSoundService.start(); 來自onCreate方法,所以當onStart被調用時音樂就會開始。 但是,當我這樣做時,我得到了同樣的錯誤。

那么,如何在使用backgroundSoundService調用其方法之前對其進行初始化?

首先從 onCreate 中刪除這個backgroundSoundService.start()並將其添加到onServiceConnected()方法中

在做任何與 backgroundSoundService 相關的東西之前,你需要檢查 null

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

backgroundSoundService所有外觀中添加這種空檢查

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM