I am looking to implement Google\'s ExoPlayer
in my app. Their documentation seems pretty vague to me, and all I am looking for is to play a video from an URL, no c
//Add dependency in manifest file
implementation 'com.google.android.exoplayer:exoplayer:2.7.3'
// Add exoplayer in your layout(xml) file
// open video with below code
// initialise varible
PlayerView videoFullScreenPlayer;
SimpleExoPlayer player;
ProgressBar spinnerVideoDetails;
// find Id
videoFullScreenPlayer = findViewById(R.id.videoFullScreenPlayer);
spinnerVideoDetails = findViewById(R.id.spinnerVideoDetails);
// open video method
private void setUp() {
initializePlayer();
if (videoUrl == null) {
return;
}
buildMediaSource(Uri.parse(videoUrl ));
}
private void initializePlayer() {
if (player == null) {
BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory(bandwidthMeter);
TrackSelector trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);
// 1. Create a default TrackSelector
DefaultLoadControl loadControl = new DefaultLoadControl.Builder().setBufferDurationsMs(32*1024, 64*1024, 1024, 1024).createDefaultLoadControl();
// 2. Create the player
player = ExoPlayerFactory.newSimpleInstance(this, trackSelector, loadControl);
videoFullScreenPlayer.setPlayer(player);
}
}
private void buildMediaSource(Uri mUri) {
// Measures bandwidth during playback. Can be null if not required.
DefaultBandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
// Produces DataSource instances through which media data is loaded.
DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(this,
Util.getUserAgent(this, getString(R.string.app_name)), bandwidthMeter);
// This is the MediaSource representing the media to be played.
MediaSource videoSource = new ExtractorMediaSource.Factory(dataSourceFactory).createMediaSource(mUri);
// Prepare the player with the source.
player.prepare(videoSource);
player.setPlayWhenReady(true);
player.addListener(this);
}
private void releasePlayer() {
if (player != null) {
player.release();
player = null;
}
}
private void pausePlayer() {
if (player != null) {
player.setPlayWhenReady(false);
player.getPlaybackState();
}
}
private void resumePlayer() {
if (player != null) {
player.setPlayWhenReady(true);
player.getPlaybackState();
}
}
@Override
protected void onPause() {
super.onPause();
pausePlayer();
/* if (mRunnable != null) {
mHandler.removeCallbacks(mRunnable);
}*/
}
@Override
protected void onRestart() {
super.onRestart();
resumePlayer();
}
@Override
protected void onDestroy() {
super.onDestroy();
releasePlayer();
}
@Override
public void onTimelineChanged(Timeline timeline, Object manifest, int reason) {
}
@Override
public void onTracksChanged(TrackGroupArray trackGroups, TrackSelectionArray trackSelections) {
}
@Override
public void onLoadingChanged(boolean isLoading) {
}
@Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
switch (playbackState) {
case Player.STATE_BUFFERING:
spinnerVideoDetails.setVisibility(View.VISIBLE);
break;
case Player.STATE_ENDED:
// Activate the force enable
break;
case Player.STATE_IDLE:
break;
case Player.STATE_READY:
spinnerVideoDetails.setVisibility(View.GONE);
break;
default:
// status = PlaybackStatus.IDLE;
break;
}
}
@Override
public void onRepeatModeChanged(int repeatMode) {
}
@Override
public void onShuffleModeEnabledChanged(boolean shuffleModeEnabled) {
}
@Override
public void onPlayerError(ExoPlaybackException error) {
}
@Override
public void onPositionDiscontinuity(int reason) {
}
@Override
public void onPlaybackParametersChanged(PlaybackParameters playbackParameters) {
}
@Override
public void onSeekProcessed() {
}