TubiExoPlayerView.java


Overview

`TubiExoPlayerView` is a custom Android UI component extending `FrameLayout` that integrates Google’s ExoPlayer video playback within a flexible, reusable player view. It serves as a comprehensive container managing:

This class facilitates the display and interaction aspects of media playback by combining video rendering, subtitles, and user controls into a single encapsulated view, improving modularity and consistency across the app.


Class: TubiExoPlayerView

Description

The main class of the file, `TubiExoPlayerView`, is a highly configurable player view embedding ExoPlayer playback components. It manages the lifecycle of the video surface and subtitles, and provides interfaces for setting the player and custom user controls.

Key Fields

Field

Type

Description

`contentFrame`

`AspectRatioFrameLayout`

Container that maintains video aspect ratio dynamically.

`shutterView`

`View`

A view overlay shown while video is loading or the first frame is not rendered yet.

`surfaceView`

`View`

The video rendering surface, either `SurfaceView` or `TextureView`.

`subtitleView`

`SubtitleView`

Renders timed subtitles/captions synchronized with video playback.

`mUserInteractionView`

`View`

Custom user interaction controls (play/pause, seek, etc.) injected dynamically.

`componentListener`

`ComponentListener`

Internal listener handling video, text, and player events.

`player`

`SimpleExoPlayer`

The ExoPlayer instance associated with this view.

`userController`

`UserController`

Playback control interface managing user interaction and player state.

Constructors

**Functionality:**

Public Methods

void addUserInteractionView(@Nullable View controlView)

Adds a playback control view to a placeholder within the player layout.

View customControls = LayoutInflater.from(context).inflate(R.layout.custom_controls, null);
tubiExoPlayerView.addUserInteractionView(customControls);

SimpleExoPlayer getPlayer()

Returns the currently associated ExoPlayer instance or null if none.

void setPlayer(SimpleExoPlayer player, @NonNull PlaybackActionCallback playbackActionCallback)

Binds a `SimpleExoPlayer` to this view, managing video surface assignment and event listeners.

SimpleExoPlayer player = new SimpleExoPlayer.Builder(context).build();
tubiExoPlayerView.setPlayer(player, playbackActionCallback);

void setResizeMode(@AspectRatioFrameLayout.ResizeMode int resizeMode)

Sets the video content resize mode on the aspect ratio container.

tubiExoPlayerView.setResizeMode(AspectRatioFrameLayout.RESIZE_MODE_FILL);

SubtitleView getSubtitleView()

Returns the `SubtitleView` used for rendering subtitles, or null if not present.

void setMediaModel(@NonNull MediaModel mediaModel)

Sets the media metadata on the internal `UserController` to update UI state accordingly.

void setAvailableAdLeft(int count)

Updates the number of available ads remaining (used by `UserController` to update UI).


Inner Class: ComponentListener

Implements the following interfaces to listen to player events and update UI components:

Responsibilities


Important Implementation Details


Interaction with Other System Components


Usage Example

// Create the player view
TubiExoPlayerView playerView = new TubiExoPlayerView(context);

// Inflate and add custom controls
View controls = LayoutInflater.from(context).inflate(R.layout.custom_controls, null);
playerView.addUserInteractionView(controls);

// Initialize ExoPlayer instance
SimpleExoPlayer player = new SimpleExoPlayer.Builder(context).build();

// Set player and callback
playerView.setPlayer(player, playbackActionCallback);

// Set media metadata
MediaModel mediaModel = new MediaModel(...);
playerView.setMediaModel(mediaModel);

// Add playerView to your layout container
parentLayout.addView(playerView);

Mermaid Class Diagram

classDiagram
    class TubiExoPlayerView {
        -AspectRatioFrameLayout contentFrame
        -View shutterView
        -View surfaceView
        -SubtitleView subtitleView
        -View mUserInteractionView
        -SimpleExoPlayer player
        -UserController userController
        +TubiExoPlayerView(Context, AttributeSet, int)
        +addUserInteractionView(View)
        +setPlayer(SimpleExoPlayer, PlaybackActionCallback)
        +getPlayer() SimpleExoPlayer
        +setResizeMode(int)
        +getSubtitleView() SubtitleView
        +setMediaModel(MediaModel)
        +setAvailableAdLeft(int)
    }

    class ComponentListener {
        +onCues(List~Cue~)
        +onVideoSizeChanged(int, int, int, float)
        +onRenderedFirstFrame()
        +onPlayerStateChanged(boolean, int)
        +onPlayerError(ExoPlaybackException)
        ...
    }

    TubiExoPlayerView o-- "1" SimpleExoPlayer : manages
    TubiExoPlayerView o-- "1" AspectRatioFrameLayout : manages layout
    TubiExoPlayerView o-- "1" SubtitleView : renders subtitles
    TubiExoPlayerView o-- "0..1" View : user interaction controls
    TubiExoPlayerView --> UserController : delegates control and state
    TubiExoPlayerView *-- ComponentListener : listens to player events

Summary

`TubiExoPlayerView` is a foundational UI component designed for integrating ExoPlayer video playback with subtitle rendering and user controls in a modular, extensible manner. It abstracts video surface management, adapts to device form factors, and provides seamless binding between player events and UI updates. By delegating playback control logic to `UserController` and allowing dynamic control view injection, it supports diverse playback scenarios and UI customizations within the Tubi media playback ecosystem.