Skip to main content

Overview

The ExoPlayerUtil class provides utilities for managing video playback using ExoPlayer with built-in caching support. It handles player initialization, media playback control, and resource management.

Initialization

init

Initializes the ExoPlayer instance with the application context.
ExoPlayerUtil.init(context);
context
Context
required
The Android application context

getExoplayer

Returns the singleton ExoPlayer instance.
ExoPlayer player = ExoPlayerUtil.getExoplayer();
Returns: ExoPlayer - The initialized ExoPlayer instance

Playback Methods

play

Plays a video from a URI with caching support.
ExoPlayerUtil.play(videoUri, playerView);
uri
Uri
required
The URI of the video to play
playerView
PlayerView
required
The PlayerView to attach the player to
Example:
Uri videoUri = Uri.parse("https://example.com/video.mp4");
PlayerView playerView = findViewById(R.id.player_view);
ExoPlayerUtil.play(videoUri, playerView);

play (with preview)

Plays a video with an optional preview ImageView.
ExoPlayerUtil.play(videoUri, playerView, previewImageView);
uri
Uri
required
The URI of the video to play
playerView
PlayerView
required
The PlayerView to attach the player to
previewImageView
ImageView
required
The ImageView for displaying a preview thumbnail

play (from File)

Plays a video from a local file.
File videoFile = new File("/path/to/video.mp4");
ExoPlayerUtil.play(videoFile, playerView);
file
File
required
The video file to play
playerView
PlayerView
required
The PlayerView to attach the player to

playFromLocalUri

Plays a video from a local URI without caching.
ExoPlayerUtil.playFromLocalUri(localUri, playerView);
uri
Uri
required
The local URI of the video to play
playerView
PlayerView
required
The PlayerView to attach the player to

playNoLoop

Plays a video without looping.
ExoPlayerUtil.playNoLoop(videoUri, playerView);
uri
Uri
required
The URI of the video to play
playerView
PlayerView
required
The PlayerView to attach the player to
Note: By default, the player is set to REPEAT_MODE_ONE. This method sets it to REPEAT_MODE_OFF.

Playback Control

pause

Pauses the current playback.
ExoPlayerUtil.pause();

resume

Resumes the paused playback.
ExoPlayerUtil.resume();

stop

Stops the current playback.
ExoPlayerUtil.stop();

seekTo

Seeks to a specific position in the video.
ExoPlayerUtil.seekTo(30000); // Seek to 30 seconds
position
long
required
The position to seek to in milliseconds

Audio Control

mute

Mutes the audio.
ExoPlayerUtil.mute();

unMute

Unmutes the audio.
ExoPlayerUtil.unMute();

Playback Information

getPlayingInfo

Returns information about the current playback.
long[] info = ExoPlayerUtil.getPlayingInfo();
long duration = info[0];
long currentPosition = info[1];
Returns: long[] - Array with [duration, currentPosition] in milliseconds Example:
long[] info = ExoPlayerUtil.getPlayingInfo();
long duration = info[0];
long currentPosition = info[1];
int progress = (int) ((currentPosition * 100) / duration);

Caching & Optimization

preloadReel

Preloads a video for smooth playback later.
ExoPlayerUtil.preloadReel(videoUri);
uri
Uri
required
The URI of the video to preload
Example:
// Preload the next video in a feed
Uri nextVideoUri = Uri.parse("https://example.com/next-video.mp4");
ExoPlayerUtil.preloadReel(nextVideoUri);

Resource Management

release

Releases the ExoPlayer instance and frees resources.
ExoPlayerUtil.release();
Important: Call this method in your Activity’s onDestroy() to prevent memory leaks. Example:
@Override
protected void onDestroy() {
    super.onDestroy();
    ExoPlayerUtil.release();
}

ExoPlayerCache

The ExoPlayerCache class manages video caching for offline playback and improved performance.

getInstance

Returns the singleton cache instance.
SimpleCache cache = ExoPlayerCache.getInstance(context);
context
Context
required
The Android application context
Returns: SimpleCache - A SimpleCache instance with 1000 MB capacity Cache Configuration:
  • Cache directory: app_cache/exo_media_cache
  • Cache size: 1000 MB
  • Eviction policy: Least Recently Used (LRU)

Complete Example

public class VideoPlayerActivity extends AppCompatActivity {
    private PlayerView playerView;
    private Uri videoUri;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_player);

        playerView = findViewById(R.id.player_view);
        videoUri = Uri.parse("https://example.com/video.mp4");

        // Initialize the player
        ExoPlayerUtil.init(this);

        // Start playback
        ExoPlayerUtil.play(videoUri, playerView);
    }

    @Override
    protected void onPause() {
        super.onPause();
        ExoPlayerUtil.pause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        ExoPlayerUtil.resume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ExoPlayerUtil.release();
    }
}

Notes

  • The player is initialized with REPEAT_MODE_ONE by default
  • All network playback methods use caching through CacheDataSourceUtil
  • The player automatically detaches from previous PlayerViews when playing new content
  • Methods marked with @UnstableApi require the AndroidX Media3 library

Build docs developers (and LLMs) love