暫停和還原MediaPlayer

上次更新: 2023-09-21

裝置熒幕關閉和開啟時,您必須由應用程式處理,才能暫停及還原TVSDK MediaPlayer。

您可以處理暫停和還原操作 MediaPlayer 在Android的廣播接收器內,用於開啟/關閉熒幕。

TVSDK無法判斷片段(或活動)何時在背景或前景。 此外,Android SurfaceView 裝置畫面關閉時不會遭到破壞(但活動已暫停)。 不過, SurfaceView 當裝置將您的應用程式置於背景時,就會遭到破壞。 TVSDK無法偵測到任何這些變更,因此應用程式必須處理這些變更。

以下範常式式碼您的應用程式如何處理暫停和還原 MediaPlayer 在應用程式層級開啟和關閉裝置畫面時:

// Track the state of a fragment to determine if it is PAUSED or RESUMED
private boolean isActivityPaused = false;

/**
* Register the broadcast receiver to track screen on/screen off functions triggered from
device.
*/
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {

        // Call suspend when screen is turned off and mediaPlayer is not null and
        // mediaplayer status is not suspended/Error/Released state.
        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
            try {
                if (mediaPlayer != null &&
                  lastKnownStatus != MediaPlayerStatus.ERROR &&
                  lastKnownStatus != MediaPlayerStatus.RELEASED &&
                  lastKnownStatus != MediaPlayerStatus.SUSPENDED) {
                    PrimetimeReference.logger.i(LOG_TAG+"#screenOff:",
                    "Suspending mediaplayer as screen is turned off and mediaPlayer
                    status is " + lastKnownStatus.toString());
                    mediaPlayer.suspend();
                }
                else {
                    PrimetimeReference.logger.i(LOG_TAG+"#screenOff:",
                      "Not suspending mediaplayer since mediaplayer status is "
                      + lastKnownStatus.toString());
                }
            } catch(MediaPlayerException e) {
                PrimetimeReference.logger.e(LOG_TAG+"#screenOff:",
                  "MediaPlayer Exception for suspend() call");
            }
        }
        else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
            // Call restore when the screen is turned on and mediaplayer is not in the
            // suspended status.  This is for the screen on condition when the device
            // does not have a lock and turning on the screen immediately brings the
            // fragment to the foreground.
            try {
                if(lastKnownStatus == MediaPlayerStatus.SUSPENDED && !isActivityPaused) {
                    PrimetimeReference.logger.i(LOG_TAG+"#screenOn:",
                      "Restoring mediaplayer since screen is turned on and mediaPlayer status is "
                      + lastKnownStatus.toString());
                    mediaPlayer.restore();
                }
                else {
                    PrimetimeReference.logger.i(LOG_TAG+"#screenOn:",
                      "Not restoring mediaplayer since mediaPlayer status is "
                      + lastKnownStatus.toString());
                }
            } catch(MediaPlayerException e) {
                PrimetimeReference.logger.e(LOG_TAG+"#screenOn:",
                  "MediaPlayer Exception for restore() call");
            }
        }
    }
};

/*
* Activity or Fragment's onPause() overridden method
*/
@Override
public void onPause() {
    PrimetimeReference.logger.i(LOG_TAG + "#onPause", "Player activity paused.");

    // Set the fragment paused status to true when app goes in background.
    isActivityPaused = true;
    super.onPause();
}

/*
* Activity or Fragment's onResume() overridden method
*/
@Override
public void onResume() {
    super.onResume();

    /**
    * When the device has a lock/pin the on resume will be called only after the device
      is unlocked.
    * Screen on does not call the onResume() method so we need to handle restore here
      explicitly.
    */
    if(lastKnownStatus == MediaPlayerStatus.SUSPENDED && isActivityPaused) {
        try {
            PrimetimeReference.logger.i(LOG_TAG + "#onResume",
              "Player restored as activity operations are resumed");
            mediaPlayer.restore();
        }
        catch(MediaPlayerException e) {
            PrimetimeReference.logger.i(LOG_TAG + "#onResume",
              "Exception occured while restoring mediaPlayer");
        }
    }
    // Set the fragment paused status to false when app comes in foreground.
    isActivityPaused = false;
}

此頁面上的