PeterPakla
PeterPakla

Reputation: 179

Enabling background playback on Android TV

I want to keep playback in the background when the user decides to press 'home' during the movie.

I followed the guide here: https://developer.android.com/training/tv/playback/options.html

And wrote following code (WHICH WORKS):

@Override
public void onPause() {
    super.onPause();
    if (mVideoView.isPlaying())
    {
        // Argument equals true to notify the system that the activity
        // wishes to be visible behind other translucent activities
        if (! requestVisibleBehind(true)) {
            // App-specific method to stop playback and release resources
            // because call to requestVisibleBehind(true) failed
            stopPlayback();
        }
    } else {
        // Argument equals false because the activity is not playing
        requestVisibleBehind(false);
    }
}

@Override
public void onVisibleBehindCanceled() {
  // App-specific method to stop playback and release resources
  stopPlayback();
  super.onVisibleBehindCanceled();
}

I've few questions. I found that commenting out line if (! requestVisibleBehind(true)) doesn't return wanted results. I was quite confused.

  1. Shouldn't that line return some boolean value? How can it enable background playback? With the debugger, mentioned "if" is successful and it stops the player. Not really sure what's happening and how, so maybe someone can explain me.
  2. requestVisibleBehind() is @deprecated, so is onVisibleBehindCanceled(). Is there any alternatives? When I tried to look into what these functions hold, I also surprised myself when I found (same goes for requestVisibleBehind().

    public void onVisibleBehindCanceled() 
    {
        throw new RuntimeException("Stub!");
    }
    

At one point, I was pretty sure that background playback result is from other actions, but when I comment out code fragmented previously mentioned, I don't get wanted result (which I DO get if I don't comment it).

Upvotes: 7

Views: 1265

Answers (1)

Ernesto Ulloa
Ernesto Ulloa

Reputation: 482

i don't know if i understand correctly what you are asking. According to google the requestVisibleBehind(true) requests to android OS that in case of going to the background this activity should continue to play, the boolean it returns is if you can count on it being available to continue playing or not(that is decided by the OS). Regarding onVisibleBehindCanceled() it's called when the app is finished either by the user or the OS or when the requestVisibleBehind returned false and essentially what it does is give you 500ms to dispose all resources being used by your app, after those 500ms the OS will destroy your activity in order to free those resources. So in summary those are indeed the ones responsible for your app continue to play on the background. Regarding the deprecation, remember that android O comes with the ability to use PIP so maybe that's the reason for this change.

Hope it helped

Upvotes: 3

Related Questions