Class PlayerView

  • All Implemented Interfaces:
    Drawable.Callback, AccessibilityEventSource, KeyEvent.Callback, ViewManager, ViewParent, IPlayerView

    public class PlayerView
    extends FrameLayout
    implements IPlayerView
    The CastLabs PlayerView is a custom view that provides the video playback surface.

    You can add this view to an activity and access the PlayerController to gain control over the player.

    Note that the view needs to react to certain activity lifecycle events to be able to start, stop, and resume playback when the activity is paused or resumed. The underlying PlayerController also needs to be shut down when the hosting activity is destroyed.

    The view offers a lifecycle helper class (see PlayerViewLifecycleDelegate) to simplify the integration, but you will need to hook up the events in the hosting activity. For this, overwrite the following methods in the hosting activity and delegate to the lifecycle listener:

    
     @Override
      protected void onStart() {
          super.onStart();
          playerView.getLifecycleDelegate().start(this);
      }
    
     @Override
      protected void onResume() {
          super.onResume();
          playerView.getLifecycleDelegate().resume();
      }
    
     @Override
      protected void onStop() {
          super.onStop();
          playerView.getLifecycleDelegate().releasePlayer(false);
      }
     
    Please note that the example above does not deal with background playback. Please take a look at PlayerService to see how to integrate the lifecycle events when you are using a service and want to enable background playback. Note also the PlayerViewLifecycleDelegate does not deal with the use-case when the activity is destroyed and then re-created with the saved state. For this to work, the client application should save the state manually in `onSaveInstanceState` and then use it when the activity is re-created in `onCreate`:

    
    
     @Override
     public void onSaveInstanceState(Bundle outState) {
                    Bundle savedStateBundle = new Bundle();
                    PlayerConfig playerConfig = playerView.getPlayerController().getPlayerConfig();
                    if (playerConfig != null) {
                            playerView.getPlayerController().getPlayerConfig().save(savedStateBundle);
                            outState.putBundle(SAVED_PLAYBACK_STATE_BUNDLE_KEY, savedStateBundle);
                    }
                    super.onSaveInstanceState(outState);
     }
    
     @Override
     protected void onCreate(Bundle savedInstanceState) {
                    ...
                    Bundle playbackBundle = null;
                    if (savedInstanceState == null) {
                            Log.d(TAG, "Opening playback initially");
                            playbackBundle = ...
                    } else {
                            Log.d(TAG, "Opening playback from the saved state bundle");
                            playbackBundle = savedInstanceState.getBundle(SAVED_PLAYBACK_STATE_BUNDLE_KEY);
                    }
                    ...
                    playerView.getPlayerController().open(playbackBundle);
     }
     
    Since:
    1.1.0
    • Field Detail

      • SURFACE_SURFACE_VIEW

        public static final int SURFACE_SURFACE_VIEW
        Indicates the use of a SurfaceView to provide the rendering surface for this player view. This is the default and allows playback of encrypted content.
        See Also:
        Constant Field Values
      • SURFACE_TEXTURE_VIEW

        public static final int SURFACE_TEXTURE_VIEW
        Indicates the use of a TextureView to provide the rendering surface for this player view. Note that this will not allow playback of encrypted content.
        See Also:
        Constant Field Values
      • SCALING_MODE_FIT

        public static final int SCALING_MODE_FIT
        The video will be scaled using its original aspect ration to fit on the devices screen. Any blank space will be filled with horizontal or vertical black bars. This is the default behaviour.
        See Also:
        Constant Field Values
      • SCALING_MODE_CROP

        public static final int SCALING_MODE_CROP
        The video will be displayed in its original size. If the video is larger than the screen size, any overflow will not be visible and cut off (cropped).
        See Also:
        Constant Field Values
      • SCALING_MODE_STRETCH

        public static final int SCALING_MODE_STRETCH
        The video will be scaled to fully utilize the available screen size. The original aspect ratio of the video will not be kept.
        See Also:
        Constant Field Values
    • Method Detail

      • measureSize

        public Pair<Integer,​Integer> measureSize​(int measuredWidth,
                                                       int measuredHeight)
      • setPlayerController

        public void setPlayerController​(@Nullable
                                        PlayerController playerController)
        Set the player controller used by this view. This is used for example when reattaching to an existing controller. Note that you usually do not need to manually call this method. When you are using a PlayerService, the service will automatically reattach the controller and call this method. When you are using the view without a service, the view will create a controller for you automatically.
        Specified by:
        setPlayerController in interface IPlayerView
        Parameters:
        playerController - The player controller
      • tryPlayerController

        @Nullable
        public PlayerController tryPlayerController()
        Description copied from interface: IPlayerView
        Returns the current instance of the PlayerController that is used by this view.

        Note that you do not create the controller lazily here.

        Specified by:
        tryPlayerController in interface IPlayerView
        Returns:
        The player controller instance that is used to render video to this view
      • getLifecycleDelegate

        @NonNull
        public PlayerViewLifecycleDelegate getLifecycleDelegate()
        Description copied from interface: IPlayerView
        This method must return an instance of the PlayerViewLifecycleDelegate. That instance can be used to integrate the view and its controller with an Activity lifecycle. Please note that the returned instance might also be used implicitly when the PlayerService is used and the player is send to background.
        Specified by:
        getLifecycleDelegate in interface IPlayerView
        Returns:
        The lifecycle helper to delegate activity lifecycle events
      • getScalingMode

        public int getScalingMode()
        Specified by:
        getScalingMode in interface IPlayerView
        Returns:
        The current scaling mode
      • setSurfaceType

        public void setSurfaceType​(int surfaceType)
        Set the surface type to one of SURFACE_SURFACE_VIEW or SURFACE_TEXTURE_VIEW to specify the type of surface that will be used to render the video content.

        This only sets the desired type, but will not actively change the type.

        Note that using SURFACE_TEXTURE_VIEW and a TextureView will not allow you to playback encrypted and protected content.

        Parameters:
        surfaceType - the surface type
      • removeSurface

        public void removeSurface()
        Description copied from interface: IPlayerView
        This method is called by the PlayerController once the player is released. Implementations should remove the video surface if it exists.
        Specified by:
        removeSurface in interface IPlayerView
      • setSurfaceVisibility

        public void setSurfaceVisibility​(boolean visible)
        If you pass true to this method, the current surface view will be scaled to zero size to hide it from the screen but keep it alive. This will allow you to reuse the same surface. This method can be used for example when you are doing ad insertion and you need to show another surface view on top of this one.
        Parameters:
        visible - the surface visibility
      • setVisible

        public void setVisible​(boolean visible)
        Description copied from interface: IPlayerView
        Allow to show ore hide this player view. This is used by the player controller in case the the player view needs to be made invisible. For example, if a secondary display is connected and content playback should be prevented, playback will stop and this method will be used hide the player view.
        Specified by:
        setVisible in interface IPlayerView
        Parameters:
        visible - True if the view should be visible
      • getRootView

        @NonNull
        public ViewGroup getRootView()
        Description copied from interface: IPlayerView
        Retrieve the root view of the current IPlayerView implementation. The video container view is added internally to the root view with the index zero. All other child views when added to the root view are on top of video container view and do not respect the video container aspect ratio.
        Specified by:
        getRootView in interface IPlayerView
        Overrides:
        getRootView in class View
        Returns:
        The root view of the current IPlayerView implementation
      • getVideoView

        @NonNull
        public ViewGroup getVideoView()
        Description copied from interface: IPlayerView
        Retrieve the video container view of the current IPlayerView implementation. All the child view when added to the video container view do respect the video container aspect ratio.
        Specified by:
        getVideoView in interface IPlayerView
        Returns:
        The video container view of the current IPlayerView implementation
      • getComponent

        @Nullable
        public <T> T getComponent​(Class<T> type)
        Returns the player view component of the given type if it exists.
        Type Parameters:
        T - The component
        Parameters:
        type - The target type
        Returns:
        The component or null
      • getComponentView

        @Nullable
        public View getComponentView​(@IdRes
                                     int viewId)
        Returns the component View linked to the provided id
        Parameters:
        viewId - the id
        Returns:
        the View