Overview
TheExoPlayerUtil 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.The Android application context
getExoplayer
Returns the singleton ExoPlayer instance.ExoPlayer - The initialized ExoPlayer instance
Playback Methods
play
Plays a video from a URI with caching support.The URI of the video to play
The PlayerView to attach the player to
play (with preview)
Plays a video with an optional preview ImageView.The URI of the video to play
The PlayerView to attach the player to
The ImageView for displaying a preview thumbnail
play (from File)
Plays a video from a local file.The video file to play
The PlayerView to attach the player to
playFromLocalUri
Plays a video from a local URI without caching.The local URI of the video to play
The PlayerView to attach the player to
playNoLoop
Plays a video without looping.The URI of the video to play
The PlayerView to attach the player to
REPEAT_MODE_ONE. This method sets it to REPEAT_MODE_OFF.
Playback Control
pause
Pauses the current playback.resume
Resumes the paused playback.stop
Stops the current playback.seekTo
Seeks to a specific position in the video.The position to seek to in milliseconds
Audio Control
mute
Mutes the audio.unMute
Unmutes the audio.Playback Information
getPlayingInfo
Returns information about the current playback.long[] - Array with [duration, currentPosition] in milliseconds
Example:
Caching & Optimization
preloadReel
Preloads a video for smooth playback later.The URI of the video to preload
Resource Management
release
Releases the ExoPlayer instance and frees resources.onDestroy() to prevent memory leaks.
Example:
ExoPlayerCache
TheExoPlayerCache class manages video caching for offline playback and improved performance.
getInstance
Returns the singleton cache instance.The Android application context
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
Notes
- The player is initialized with
REPEAT_MODE_ONEby default - All network playback methods use caching through
CacheDataSourceUtil - The player automatically detaches from previous PlayerViews when playing new content
- Methods marked with
@UnstableApirequire the AndroidX Media3 library