Class PlayerView

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

    public class PlayerView
    extends FrameLayout
    implements AdViewProvider
    A high level view for Player media playbacks. It displays video, subtitles and album art during playback, and displays playback controls using a PlayerControlView.

    A PlayerView can be customized by setting attributes (or calling corresponding methods), overriding drawables, overriding the view's layout file, or by specifying a custom view layout file.

    Attributes

    The following attributes can be set on a PlayerView when used in a layout XML file:
    • use_artwork - Whether artwork is used if available in audio streams.
    • default_artwork - Default artwork to use if no artwork available in audio streams.
    • use_controller - Whether the playback controls can be shown.
    • hide_on_touch - Whether the playback controls are hidden by touch events.
    • auto_show - Whether the playback controls are automatically shown when playback starts, pauses, ends, or fails. If set to false, the playback controls can be manually operated with showController() and hideController().
    • hide_during_ads - Whether the playback controls are hidden during ads. Controls are always shown during ads if they are enabled and the player is paused.
    • show_buffering - Whether the buffering spinner is displayed when the player is buffering. Valid values are never, when_playing and always.
    • resize_mode - Controls how video and album art is resized within the view. Valid values are fit, fixed_width, fixed_height, fill and zoom.
    • surface_type - The type of surface view used for video playbacks. Valid values are surface_view, texture_view, spherical_gl_surface_view, video_decoder_gl_surface_view and none. Using none is recommended for audio only applications, since creating the surface can be expensive. Using surface_view is recommended for video applications. Note, TextureView can only be used in a hardware accelerated window. When rendered in software, TextureView will draw nothing.
      • Corresponding method: None
      • Default: surface_view
    • shutter_background_color - The background color of the exo_shutter view.
    • keep_content_on_player_reset - Whether the currently displayed video frame or media artwork is kept visible when the player is reset.
    • player_layout_id - Specifies the id of the layout to be inflated. See below for more details.
      • Corresponding method: None
      • Default: R.layout.exo_player_view
    • controller_layout_id - Specifies the id of the layout resource to be inflated by the child PlayerControlView. See below for more details.
      • Corresponding method: None
      • Default: R.layout.exo_player_control_view
    • All attributes that can be set on PlayerControlView and DefaultTimeBar can also be set on a PlayerView, and will be propagated to the inflated PlayerControlView unless the layout is overridden to specify a custom exo_controller (see below).

    Overriding drawables

    The drawables used by PlayerControlView (with its default layout file) can be overridden by drawables with the same names defined in your application. See the PlayerControlView documentation for a list of drawables that can be overridden.

    Overriding the layout file

    To customize the layout of PlayerView throughout your app, or just for certain configurations, you can define exo_player_view.xml layout files in your application res/layout* directories. These layouts will override the one provided by the library, and will be inflated for use by PlayerView. The view identifies and binds its children by looking for the following ids:
    • exo_content_frame - A frame whose aspect ratio is resized based on the video or album art of the media being played, and the configured resize_mode. The video surface view is inflated into this frame as its first child.
    • exo_shutter - A view that's made visible when video should be hidden. This view is typically an opaque view that covers the video surface, thereby obscuring it when visible. Obscuring the surface in this way also helps to prevent flicker at the start of playback when surface_type="surface_view".
    • exo_buffering - A view that's made visible when the player is buffering. This view typically displays a buffering spinner or animation.
    • exo_subtitles - Displays subtitles.
    • exo_artwork - Displays album art.
    • exo_error_message - Displays an error message to the user if playback fails.
    • exo_controller_placeholder - A placeholder that's replaced with the inflated PlayerControlView. Ignored if an exo_controller view exists.
    • exo_controller - An already inflated PlayerControlView. Allows use of a custom extension of PlayerControlView. PlayerControlView and DefaultTimeBar attributes set on the PlayerView will not be automatically propagated through to this instance. If a view exists with this id, any exo_controller_placeholder view will be ignored.
    • exo_ad_overlay - A FrameLayout positioned on top of the player which is used to show ad UI (if applicable).
    • exo_overlay - A FrameLayout positioned on top of the player which the app can access via getOverlayFrameLayout(), provided for convenience.

    All child views are optional and so can be omitted if not required, however where defined they must be of the expected type.

    Specifying a custom layout file

    Defining your own exo_player_view.xml is useful to customize the layout of PlayerView throughout your application. It's also possible to customize the layout for a single instance in a layout file. This is achieved by setting the player_layout_id attribute on a PlayerView. This will cause the specified layout to be inflated instead of exo_player_view.xml for only the instance on which the attribute is set.
    • Field Detail

      • SHOW_BUFFERING_NEVER

        public static final int SHOW_BUFFERING_NEVER
        The buffering view is never shown.
        See Also:
        Constant Field Values
      • SHOW_BUFFERING_ALWAYS

        public static final int SHOW_BUFFERING_ALWAYS
        The buffering view is always shown when the player is in the buffering state.
        See Also:
        Constant Field Values
    • Constructor Detail

      • PlayerView

        public PlayerView​(Context context)
      • PlayerView

        public PlayerView​(Context context,
                          @Nullable
                          AttributeSet attrs,
                          int defStyleAttr)
    • Method Detail

      • switchTargetView

        public static void switchTargetView​(Player player,
                                            @Nullable
                                            PlayerView oldPlayerView,
                                            @Nullable
                                            PlayerView newPlayerView)
        Switches the view targeted by a given Player.
        Parameters:
        player - The player whose target view is being switched.
        oldPlayerView - The old view to detach from the player.
        newPlayerView - The new view to attach to the player.
      • getPlayer

        @Nullable
        public Player getPlayer()
        Returns the player currently set on this view, or null if no player is set.
      • setPlayer

        public void setPlayer​(@Nullable
                              Player player)
        Sets the Player to use.

        To transition a Player from targeting one view to another, it's recommended to use switchTargetView(Player, PlayerView, PlayerView) rather than this method. If you do wish to use this method directly, be sure to attach the player to the new view before calling setPlayer(null) to detach it from the old one. This ordering is significantly more efficient and may allow for more seamless transitions.

        Parameters:
        player - The Player to use, or null to detach the current player. Only players which are accessed on the main thread are supported ( player.getApplicationLooper() == Looper.getMainLooper()).
      • setVisibility

        public void setVisibility​(int visibility)
        Overrides:
        setVisibility in class View
      • getUseArtwork

        public boolean getUseArtwork()
        Returns whether artwork is displayed if present in the media.
      • setUseArtwork

        public void setUseArtwork​(boolean useArtwork)
        Sets whether artwork is displayed if present in the media.
        Parameters:
        useArtwork - Whether artwork is displayed.
      • getDefaultArtwork

        @Nullable
        public Drawable getDefaultArtwork()
        Returns the default artwork to display.
      • setDefaultArtwork

        public void setDefaultArtwork​(@Nullable
                                      Drawable defaultArtwork)
        Sets the default artwork to display if useArtwork is true and no artwork is present in the media.
        Parameters:
        defaultArtwork - the default artwork to display
      • getUseController

        public boolean getUseController()
        Returns whether the playback controls can be shown.
      • setUseController

        public void setUseController​(boolean useController)
        Sets whether the playback controls can be shown. If set to false the playback controls are never visible and are disconnected from the player.
        Parameters:
        useController - Whether the playback controls can be shown.
      • setShutterBackgroundColor

        public void setShutterBackgroundColor​(int color)
        Sets the background color of the exo_shutter view.
        Parameters:
        color - The background color.
      • setKeepContentOnPlayerReset

        public void setKeepContentOnPlayerReset​(boolean keepContentOnPlayerReset)
        Sets whether the currently displayed video frame or media artwork is kept visible when the player is reset. A player reset is defined to mean the player being re-prepared with different media, the player transitioning to unprepared media or an empty list of media items, or the player being replaced or cleared by calling setPlayer(Player).

        If enabled, the currently displayed video frame or media artwork will be kept visible until the player set on the view has been successfully prepared with new media and loaded enough of it to have determined the available tracks. Hence enabling this option allows transitioning from playing one piece of media to another, or from using one player instance to another, without clearing the view's content.

        If disabled, the currently displayed video frame or media artwork will be hidden as soon as the player is reset. Note that the video frame is hidden by making exo_shutter visible. Hence the video frame will not be hidden if using a custom layout that omits this view.

        Parameters:
        keepContentOnPlayerReset - Whether the currently displayed video frame or media artwork is kept visible when the player is reset.
      • setCustomErrorMessage

        public void setCustomErrorMessage​(@Nullable
                                          CharSequence message)
        Sets a custom error message to be displayed by the view. The error message will be displayed permanently, unless it is cleared by passing null to this method.
        Parameters:
        message - The message to display, or null to clear a previously set message.
      • dispatchMediaKeyEvent

        public boolean dispatchMediaKeyEvent​(KeyEvent event)
        Called to process media key events. Any KeyEvent can be passed but only media key events will be handled. Does nothing if playback controls are disabled.
        Parameters:
        event - A key event.
        Returns:
        Whether the key event was handled.
      • isControllerVisible

        public boolean isControllerVisible()
        Returns whether the controller is currently visible.
      • showController

        public void showController()
        Shows the playback controls. Does nothing if playback controls are disabled.

        The playback controls are automatically hidden during playback after {getControllerShowTimeoutMs()}. They are shown indefinitely when playback has not started yet, is paused, has ended or failed.

      • hideController

        public void hideController()
        Hides the playback controls. Does nothing if playback controls are disabled.
      • getControllerShowTimeoutMs

        public int getControllerShowTimeoutMs()
        Returns the playback controls timeout. The playback controls are automatically hidden after this duration of time has elapsed without user input and with playback or buffering in progress.
        Returns:
        The timeout in milliseconds. A non-positive value will cause the controller to remain visible indefinitely.
      • setControllerShowTimeoutMs

        public void setControllerShowTimeoutMs​(int controllerShowTimeoutMs)
        Sets the playback controls timeout. The playback controls are automatically hidden after this duration of time has elapsed without user input and with playback or buffering in progress.
        Parameters:
        controllerShowTimeoutMs - The timeout in milliseconds. A non-positive value will cause the controller to remain visible indefinitely.
      • getControllerHideOnTouch

        public boolean getControllerHideOnTouch()
        Returns whether the playback controls are hidden by touch events.
      • setControllerHideOnTouch

        public void setControllerHideOnTouch​(boolean controllerHideOnTouch)
        Sets whether the playback controls are hidden by touch events.
        Parameters:
        controllerHideOnTouch - Whether the playback controls are hidden by touch events.
      • getControllerAutoShow

        public boolean getControllerAutoShow()
        Returns whether the playback controls are automatically shown when playback starts, pauses, ends, or fails. If set to false, the playback controls can be manually operated with showController() and hideController().
      • setControllerAutoShow

        public void setControllerAutoShow​(boolean controllerAutoShow)
        Sets whether the playback controls are automatically shown when playback starts, pauses, ends, or fails. If set to false, the playback controls can be manually operated with showController() and hideController().
        Parameters:
        controllerAutoShow - Whether the playback controls are allowed to show automatically.
      • setControllerHideDuringAds

        public void setControllerHideDuringAds​(boolean controllerHideDuringAds)
        Sets whether the playback controls are hidden when ads are playing. Controls are always shown during ads if they are enabled and the player is paused.
        Parameters:
        controllerHideDuringAds - Whether the playback controls are hidden when ads are playing.
      • setShowRewindButton

        public void setShowRewindButton​(boolean showRewindButton)
        Sets whether the rewind button is shown.
        Parameters:
        showRewindButton - Whether the rewind button is shown.
      • setShowFastForwardButton

        public void setShowFastForwardButton​(boolean showFastForwardButton)
        Sets whether the fast forward button is shown.
        Parameters:
        showFastForwardButton - Whether the fast forward button is shown.
      • setShowPreviousButton

        public void setShowPreviousButton​(boolean showPreviousButton)
        Sets whether the previous button is shown.
        Parameters:
        showPreviousButton - Whether the previous button is shown.
      • setShowNextButton

        public void setShowNextButton​(boolean showNextButton)
        Sets whether the next button is shown.
        Parameters:
        showNextButton - Whether the next button is shown.
      • setShowShuffleButton

        public void setShowShuffleButton​(boolean showShuffleButton)
        Sets whether the shuffle button is shown.
        Parameters:
        showShuffleButton - Whether the shuffle button is shown.
      • setShowMultiWindowTimeBar

        public void setShowMultiWindowTimeBar​(boolean showMultiWindowTimeBar)
        Sets whether the time bar should show all windows, as opposed to just the current one.
        Parameters:
        showMultiWindowTimeBar - Whether to show all windows.
      • setExtraAdGroupMarkers

        public void setExtraAdGroupMarkers​(@Nullable
                                           long[] extraAdGroupTimesMs,
                                           @Nullable
                                           boolean[] extraPlayedAdGroups)
        Sets the millisecond positions of extra ad markers relative to the start of the window (or timeline, if in multi-window mode) and whether each extra ad has been played or not. The markers are shown in addition to any ad markers for ads in the player's timeline.
        Parameters:
        extraAdGroupTimesMs - The millisecond timestamps of the extra ad markers to show, or null to show no extra ad markers.
        extraPlayedAdGroups - Whether each ad has been played, or null to show no extra ad markers.
      • getVideoSurfaceView

        @Nullable
        public View getVideoSurfaceView()
        Gets the view onto which video is rendered. This is a:
        • SurfaceView by default, or if the surface_type attribute is set to surface_view.
        • TextureView if surface_type is texture_view.
        • SphericalGLSurfaceView if surface_type is spherical_gl_surface_view.
        • VideoDecoderGLSurfaceView if surface_type is video_decoder_gl_surface_view.
        • null if surface_type is none.
        Returns:
        The SurfaceView, TextureView, SphericalGLSurfaceView, VideoDecoderGLSurfaceView or null.
      • getOverlayFrameLayout

        @Nullable
        public FrameLayout getOverlayFrameLayout()
        Gets the overlay FrameLayout, which can be populated with UI elements to show on top of the player.
        Returns:
        The overlay FrameLayout, or null if the layout has been customized and the overlay is not present.
      • getSubtitleView

        @Nullable
        public SubtitleView getSubtitleView()
        Gets the SubtitleView.
        Returns:
        The SubtitleView, or null if the layout has been customized and the subtitle view is not present.
      • performClick

        public boolean performClick()
        Overrides:
        performClick in class View
      • onResume

        public void onResume()
        Should be called when the player is visible to the user, if the surface_type extends GLSurfaceView. It is the counterpart to onPause().

        This method should typically be called in Activity.onStart(), or Activity.onResume() for API versions <= 23.

      • onPause

        public void onPause()
        Should be called when the player is no longer visible to the user, if the surface_type extends GLSurfaceView. It is the counterpart to onResume().

        This method should typically be called in Activity.onStop(), or Activity.onPause() for API versions <= 23.

      • onContentAspectRatioChanged

        protected void onContentAspectRatioChanged​(@Nullable
                                                   AspectRatioFrameLayout contentFrame,
                                                   float aspectRatio)
        Called when there's a change in the desired aspect ratio of the content frame. The default implementation sets the aspect ratio of the content frame to the specified value.
        Parameters:
        contentFrame - The content frame, or null.
        aspectRatio - The aspect ratio to apply.
      • getAdOverlayInfos

        public List<AdOverlayInfo> getAdOverlayInfos()
        Description copied from interface: AdViewProvider
        Returns a list of AdOverlayInfo instances describing views that are on top of the ad view group, but that are essential for controlling playback and should be excluded from ad viewability measurements.

        Each view must be either a fully transparent overlay (for capturing touch events), or a small piece of transient UI that is essential to the user experience of playback (such as a button to pause/resume playback or a transient full-screen or cast button). For more information see the documentation for your ads loader.

        Specified by:
        getAdOverlayInfos in interface AdViewProvider