La sospensione e il ripristino di TVSDK MediaPlayer quando lo schermo di un dispositivo è spento e acceso devono essere gestiti dall'applicazione.
È possibile gestire le operazioni di sospensione e ripristino su MediaPlayer
all'interno del ricevitore di trasmissione Android per l'attivazione/disattivazione dello schermo.
TVSDK non è in grado di determinare quando un frammento (o un’attività) si trova in background o in primo piano. Inoltre, il SurfaceView
non viene distrutto quando lo schermo del dispositivo è spento (ma l’attività viene messa in pausa). Tuttavia, SurfaceView
fa vengono distrutti quando il dispositivo mette l’applicazione in background. TVSDK non è in grado di rilevare nessuna di queste modifiche, pertanto devono essere gestite dall'applicazione.
Il codice di esempio seguente spiega come l’applicazione può gestire la sospensione e il ripristino di MediaPlayer
quando lo schermo del dispositivo è acceso e spento a livello di applicazione:
// 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;
}