Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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).

like image 884
PeterPakla Avatar asked Jul 17 '17 09:07

PeterPakla


People also ask

What is background playback enabled?

Background play and autoplay are now available for both our iOS and Android apps. Go to Settings on your Curiosity Stream mobile app to enable these features. What is background play? Background play is the ability to continue listening to shows while the app is not opened on your screen.


1 Answers

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

like image 95
Ernesto Ulloa Avatar answered Oct 07 '22 18:10

Ernesto Ulloa