Class PlayerService

All Implemented Interfaces:
ComponentCallbacks, ComponentCallbacks2

public class PlayerService extends Service
This class implements a bounded Android Service that can be used to start a PlayerController in a service, send the player to background to continue audio playback, and resume video playback from a background session. The service returns PlayerService.Binder as its binder implementation.

A typical usage of this service is the following setup. You overwrite your activities onCreate() and on onResume() callbacks. In onCreate(), you acquire a reference to the PlayerView and store the passed intent to start playback later. Note that we are not interacting with the PlayerController in the onCreate() since the controller will be initialized by the service when a connection is established.


 @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      ...
      playerView = findViewById(R.id.player_View);
      playbackIntent = getIntent();
  }

 @Override
  protected void onStart() {
      super.onStart();
      playerView.getLifecycleDelegate().start(this);
  }

 @Override
 protected void onResume() {
      super.onResume();
      Intent serviceIntent = new Intent(this, PlayerService.class);
      bindService(serviceIntent, playerServiceConnection, Context.BIND_AUTO_CREATE);
  }
 
The service connection can then be implemented as follows:

 private final ServiceConnection playerServiceConnection = new ServiceConnection() {
     @Override
      public void onServiceConnected(ComponentName name, IBinder service) {
          // get the service binder and initialize the service
          playerServiceBinder = (PlayerService.Binder) service;
          boolean backgrounded = playerServiceBinder.init(playerView);

          // from now on you can use playerView.getPlayerController() to interact with the controller
          if (!backgrounded) {
              if (playbackIntent != null) {
                  // The player was not restored from background and we have a bundle to open.
                  // This indicates that we came from an onCreate() call instead of resuming
                  // an existing activity
                  playerView.getPlayerController().open(playbackIntent);
              } else {
                  // the player was not restored from background and no bundle was passed.
                  // In this case we delegate to the views lifecycle delegate to eventually restore
                  // a session
                  playerView.getLifecycleDelegate().resume();
              }
          }
          playbackIntent = null;
      }

     @Override
      public void onServiceDisconnected(ComponentName name) {
          playerServiceBinder.release(playerView, true);
          playerServiceBinder = null;
      }
 };
 

This service connection handles the three cases where

  • The controller is restored from a background session
  • No background session was found but an existing Intent exists that can be used to open playback
  • No background session was found and the Activity was resumed (here indicated by an empty playbackIntent). In this case we are delegating to the views lifecycle delegate

With the setup above, you can then overwrite the activities onStop() method to send a current session to background using the service binder.


 @Override
  protected void onStop() {
      super.onStop();
      if (playerServiceBinder != null) {
          if (!isFinishing()) {
              // The activity is not finishing but left through other means, i.e. Home button.
              // In this case, we send the player to background and display a notification
              playerServiceBinder.releaseToBackground(
                  playerView,
                  NOTIFICATION_ID,
                  createBackgroundNotification(),
                  true);
          } else {
              // We are not sending the player to background since the activity is finishing
              // so we fully release the player
              playerServiceBinder.release(playerView, true);
         }
         // in both cases we we unbind the service
         unbindService(playerServiceConnection);
         playerServiceBinder = null;
     } else {
         // We have no service connection, so we release the player without background playback
         playerView.getLifecycleDelegate().releasePlayer(false);
     }
  }
 
Note the usage of Activity.isFinishing() here. We only send the player to background if the activity is not finishing. This indicates that for example the Home button was pressed. The activity will be finishing if the user clicks the back button. In that case, we do not want to continue background playback but release the player fully.
Since:
3.0.0
  • Constructor Details

    • PlayerService

      public PlayerService()
  • Method Details