Package com.castlabs.android.player
Class PlayerViewLifecycleDelegate
java.lang.Object
com.castlabs.android.player.PlayerViewLifecycleDelegate
The default lifecycle delegate.
This class provides life cycle callbacks that can be used to manage the life cycle of the
player controller in the context of an Activity that shows a
IPlayerView
.
The default PlayerView
exposes this through the PlayerView.getLifecycleDelegate()
method. Use this to hook up activity lifecycle events.
Note that the PlayerViewLifecycleDelegate
does not handle the use-case when the Activity
is completely destroyed and re-created with the subsequent `onCreate` call. To handle this the application may
want to store the playback state upon `onSaveInstanceState` and use the state in `onCreate` call,
for more details see the PlayerView
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic interface
Callback to receive delegated Lifecycle events.static final class
Configuration object which will dictate the behaviour of thePlayerController
whenever the enclosing -
Field Summary
-
Constructor Summary
ConstructorDescriptionPlayerViewLifecycleDelegate
(IPlayerView playerView) Create a new instance using the given player view -
Method Summary
Modifier and TypeMethodDescriptionvoid
addLifecycleListener
(PlayerViewLifecycleDelegate.LifecycleListener lifecycleListener) Adds a lifecycle delegate listenervoid
releasePlayer
(boolean background_playback) Call this method when the hosting activity stops.void
removeLifecycleListener
(PlayerViewLifecycleDelegate.LifecycleListener lifecycleListener) void
resume()
Call this method when the hosting activity resumes.void
setResumeConfiguration
(PlayerViewLifecycleDelegate.ResumeConfiguration resumeConfiguration) void
Call this method when the hosting activity is started.
-
Field Details
-
DEFAULT_RESUME_CONFIGURATION
-
-
Constructor Details
-
PlayerViewLifecycleDelegate
Create a new instance using the given player view- Parameters:
playerView
- The player view
-
-
Method Details
-
start
Call this method when the hosting activity is started. This method sets up the devices screen to be kept on for the current Activity by setting the window flagFLAG_KEEP_SCREEN_ON
. If you don't want to enforce this behaviour, consider clearing the window flags after a call to this method and then manually handle the flag. If you do clear the window settings, please ensure that you set theFLAG_KEEP_SCREEN_ON
flag at least while playback is running usinggetWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
. Usually one would overwrite the activityActivity.onStart
method and delegate the event:@
Override protected void onStart() { super.onStart(); playerView.getLifecycleDelegate().start(this); }- Parameters:
activity
- The Activity that hosts the player view
-
resume
public void resume()Call this method when the hosting activity resumes. This method resumes playback from a saved state whenreleasePlayer(boolean)
was called before and player state was saved. Note that the playback of a saved state is only resumed if the current player controller is not running in background (seePlayerController.isBackgrounded()
). Usually one would overwrite the activitiesActivity.onResume()
method and delegate the event:
Please note that you should not call this method directly in the activities@
Override protected void onResume() { super.onResume(); playerView.getLifecycleDelegate().resume(); }onResume()
callback if you are using aPlayerService
. In that case, useonResume()
to (re)connect to the service and depending on the state of the service, call this method when the service is connected. -
releasePlayer
public void releasePlayer(boolean background_playback) Call this method when the hosting activity stops. The method saves the player state so playback can be resumed later, when the user navigates back to the activity. Ifbackground_playback
istrue
, the player controller will not be destroyed and should be handed off to thePlayerService
to resume it from there later. Ifbackground_playback
isfalse
, the player controller will be destroyed usingPlayerController.destroy()
. Usually one would overwrite the activitiesActivity.onStop()
method and delegate the event:@
Override protected void onStop() { super.onStop(); playerView.getLifecycleDelegate().releasePlayer(false); }- Parameters:
background_playback
- True if the player will be send to background
-
addLifecycleListener
public void addLifecycleListener(@NonNull PlayerViewLifecycleDelegate.LifecycleListener lifecycleListener) Adds a lifecycle delegate listener- Parameters:
lifecycleListener
- The listener
-
removeLifecycleListener
public void removeLifecycleListener(@NonNull PlayerViewLifecycleDelegate.LifecycleListener lifecycleListener) - Parameters:
lifecycleListener
- the Listener
-
setResumeConfiguration
public void setResumeConfiguration(@NonNull PlayerViewLifecycleDelegate.ResumeConfiguration resumeConfiguration) Sets thePlayerViewLifecycleDelegate.ResumeConfiguration
.This configuration is used whenever
resume()
is called.- Parameters:
resumeConfiguration
- the configuration to set
-