TubiPlayerActivity.java


Overview

`TubiPlayerActivity` is an **abstract base activity** that manages the lifecycle and setup of a single instance of ExoPlayer (`SimpleExoPlayer`) intended for video content playback within the Tubi media application. It provides a reusable foundation for media playback activities by:

This class can be extended directly to create a standalone video player activity with custom UI controls and support for various adaptive streaming formats.


Class: TubiPlayerActivity

Declaration

public abstract class TubiPlayerActivity extends LifeCycleActivity implements PlaybackActionCallback

Purpose

Manages ExoPlayer setup, media source construction, lifecycle-aware player release, and UI initialization. Provides hooks for subclasses to extend and customize behavior.


Fields

Field Name

Type

Description

`BANDWIDTH_METER`

`DefaultBandwidthMeter` (static final)

Monitors bandwidth to enable adaptive track selection.

`TUBI_MEDIA_KEY`

`String` (static)

Intent extra key for passing serialized `MediaModel`.

`mMoviePlayer`

`SimpleExoPlayer`

The main ExoPlayer instance used for playback.

`mTubiPlayerView`

`TubiExoPlayerView`

Custom player view managing video surface and UI controls.

`vpaidWebView`

`WebView`

WebView for rendering VPAID ads.

`cuePointIndictor`

`TextView`

UI element for showing cue point indicators during playback.

`mTrackSelector`

`DefaultTrackSelector`

Selects media tracks adaptively.

`isActive`

`boolean`

Indicates if the player is active and ready.

`mediaModel`

`MediaModel` (non-null)

Model containing metadata and URLs for the media to play.

`mMainHandler`

`Handler`

Handler for main thread tasks and callbacks.

`mMediaDataSourceFactory`

`DataSource.Factory`

Factory to create media data sources for ExoPlayer.

`mEventLogger`

`EventLogger`

Logs playback events for analytics/debugging.


Abstract Methods

Subclasses **must** implement these to provide customized behavior:

Method

Return Type

Description

`addUserInteractionView()`

`View`

Supplies a custom user interaction view to be added inside the player view for UI controls.

`onPlayerReady()`

`void`

Callback invoked once the player is ready after setup, useful for subclass initialization or UI updates.

`updateResumePosition()`

`void`

Called before player release to save or update the playback resume position (e.g., for resume playback).

`isCaptionPreferenceEnable()`

`boolean`

Returns whether subtitles/captions should be enabled by default, e.g., read from user preferences.


Lifecycle Methods and Player Management

onCreate(Bundle savedInstanceState)

parseIntent()

initLayout()

onStart() / onResume() / onPause() / onStop()

setupExo()

initMoviePlayer()

releaseMoviePlayer()


Media Source Construction

buildMediaSource(MediaModel model)

Builds appropriate ExoPlayer `MediaSource` depending on the media type:

**Throws:** `IllegalStateException` for unsupported media types.


Data Source Factory

buildDataSourceFactory(boolean useBandwidthMeter)


Caption Toggle

setCaption(boolean isOn)


Player Controller Access

getPlayerController()


Usage Example

A subclass would typically extend `TubiPlayerActivity` and implement abstract methods:

public class MyPlayerActivity extends TubiPlayerActivity {

    @Override
    public View addUserInteractionView() {
        // Inflate and return a custom controls view
        return getLayoutInflater().inflate(R.layout.my_custom_controls, null);
    }

    @Override
    protected void onPlayerReady() {
        // Perform UI updates or start playback
        mMoviePlayer.setPlayWhenReady(true);
    }

    @Override
    protected void updateResumePosition() {
        // Save current playback position for resuming later
        long position = mMoviePlayer.getCurrentPosition();
        // Persist position as needed
    }

    @Override
    protected boolean isCaptionPreferenceEnable() {
        // Return user preference for captions
        return true;
    }
}

Important Implementation Details


Interaction with Other System Components


Mermaid Class Diagram

classDiagram
    class TubiPlayerActivity {
        - static BANDWIDTH_METER: DefaultBandwidthMeter
        - static TUBI_MEDIA_KEY: String
        - mMoviePlayer: SimpleExoPlayer
        - mTubiPlayerView: TubiExoPlayerView
        - vpaidWebView: WebView
        - cuePointIndictor: TextView
        - mTrackSelector: DefaultTrackSelector
        - isActive: boolean
        - mediaModel: MediaModel
        - mMainHandler: Handler
        - mMediaDataSourceFactory: DataSource.Factory
        - mEventLogger: EventLogger
        + abstract addUserInteractionView(): View
        + abstract onPlayerReady(): void
        + abstract updateResumePosition(): void
        + abstract isCaptionPreferenceEnable(): boolean
        + onCreate(Bundle): void
        + onStart(): void
        + onResume(): void
        + onPause(): void
        + onStop(): void
        + onNewIntent(Intent): void
        + onConfigurationChanged(Configuration): void
        + isActive(): boolean
        - parseIntent(): void
        - initLayout(): void
        - setCaption(boolean): void
        - setupExo(): void
        - initMoviePlayer(): void
        - releaseMoviePlayer(): void
        - buildMediaSource(MediaModel): MediaSource
        - buildDataSourceFactory(boolean): DataSource.Factory
        + getPlayerController(): TubiPlaybackControlInterface
    }

Summary

`TubiPlayerActivity` serves as a robust and extensible foundation for video playback activities in the Tubi media app. Its design abstracts complex ExoPlayer setup and lifecycle management while empowering subclasses to customize UI and playback behavior. The class ensures smooth adaptive streaming, subtitle support, and resource-optimized playback lifecycle handling, making it a critical component in the media playback architecture.


Appendix: Activity Lifecycle Flowchart

flowchart TD
    A[onCreate] --> B[parseIntent]
    B --> C[initLayout]
    C --> D[onStart / onResume]
    D --> E[setupExo]
    E --> F[initMoviePlayer]
    F --> G[buildMediaSource]
    G --> H[bind Player to UI]
    H --> I[setCaption]
    I --> J[onPlayerReady]
    J --> K[Playback Active]
    K --> L[onPause / onStop]
    L --> M[releaseMoviePlayer]
    M --> N[updateResumePosition]
    N --> O[Playback Inactive]

If you need further details about related classes such as `TubiExoPlayerView` or `MediaModel`, or about the playback control interfaces, feel free to ask!