These are some usages of ExoPlayer.
MainActivity.java In MainActivity you can choose what you want to do. These are some more simple options.
- Play a single video;
- Play a playlist;
- Clipping a video, this will play only 5 seconds of the video;
- Loop a video 5 times.
MediaSource videoSource = new ExtractorMediaSource.Factory(dataSourceFactory)
.createMediaSource(videoUri);
// Load each video into a separated MediaSource.
MediaSource firstSource = new ExtractorMediaSource.Factory(dataSourceFactory)
.createMediaSource(videoUri1);
MediaSource secondSource = new ExtractorMediaSource.Factory(dataSourceFactory)
.createMediaSource(videoUri2);
MediaSource thirdSource = new ExtractorMediaSource.Factory(dataSourceFactory)
.createMediaSource(videoUri3);
// Concatenate the three videos to play them in sequence.
// When first video finish, second will play and so on.
ConcatenatingMediaSource playlistSource =
new ConcatenatingMediaSource(firstSource, secondSource, thirdSource);
MediaSource videoSource = new ExtractorMediaSource.Factory(dataSourceFactory)
.createMediaSource(videoUri);
// Take only from 00:10 to 00:15 seconds of the video (5 seconds total).
ClippingMediaSource clippingSource = new ClippingMediaSource(
videoSource,
10_000_000,
15_000_000);
MediaSource videoSource =
new ExtractorMediaSource.Factory(dataSourceFactory).createMediaSource(videoUri);
// Play this video 5 times.
LoopingMediaSource loopingMediaSource = new LoopingMediaSource(videoSource, 5);
ComplexActivity.java In ComplexActivity you can choose what you want to do. These are some more complex options.
- Play a mp4 or DASH video from the internet with fully player layout customization;
- Custom buttons in the player;
- Shows when the video is buffering or playing (listen to the player states);
- The video keeps playing while you rotate the device;
- Change to full screen when in landscape mode just like youtube app;
- Works in multi-window mode; (API 24 or greater)
- Come back to same video position when leaving the app and coming back again.
- You can choose Video and Audio qualities.
private MediaSource buildMediaSource(String type, Uri uri) {
DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(this,
Util.getUserAgent(this, getString(R.string.app_name)), BANDWIDTH_METER);
switch (type) {
case TYPE_DASH:
DashChunkSource.Factory dashChunkSourceFactory = new DefaultDashChunkSource.Factory(dataSourceFactory);
return new DashMediaSource.Factory(dashChunkSourceFactory, dataSourceFactory).createMediaSource(uri);
case TYPE_OTHER:
return new ExtractorMediaSource.Factory(dataSourceFactory).createMediaSource(uri);
default:{
throw new IllegalStateException("Unsupported type: " + type);
}
}
}
To customize the player layout you just need to:
- Copy the file exo_playback_control_view.xml into your app's layout folder;
- Customize the way you want;
- DO NOT change the items id, otherwise your app will not work.
Look at my exo_playback_control_view.xml to see how I customized it.
When there is no media yet (STATE_IDLE):
- Show the progressBar;
- Hide the player controllers.
When video is buffering (STATE_BUFFERING):
- Show the progressBar;
- Hide the play/pause button. (because this button is in the middle in the same place where the progressBar is placed)
When video is ready to go (STATE_READY):
- Dismiss the progressBar;
- Show the play/pause button.
private class PlayerEventListener extends Player.DefaultEventListener{
@Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
switch (playbackState) {
case Player.STATE_IDLE: // The player does not have any media to play.
mProgressBar.setVisibility(View.VISIBLE);
mPlayerView.hideController();
break;
case Player.STATE_BUFFERING: // The player needs to load media before playing.
mProgressBar.setVisibility(View.VISIBLE);
mPlayPauseLayout.setVisibility(View.GONE);
break;
case Player.STATE_READY: // The player is able to immediately play from its current position.
mProgressBar.setVisibility(View.GONE);
mPlayPauseLayout.setVisibility(View.VISIBLE);
break;
case Player.STATE_ENDED: // The player has finished playing the media.
break;
default:
break;
}
}
First, Create a ImaAdsLoader instance in onCreate method.
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_player);
...
imaAdsLoader = new ImaAdsLoader(this, getAdTagUri());
}
private Uri getAdTagUri() {
return Uri.parse("https://pubads.g.doubleclick.net/gampad/ads?sz=640x480&iu=/124319096/external/single_ad_samples&ciu_szs=300x250&impl=s&gdfp_req=1&env=vp&output=vast&unviewed_position_start=1&cust_params=deployment%3Ddevsite%26sample_ct%3Dskippablelinear&correlator=");
}
You can take more AdTagUris in here.
Second, build an AdsMediaSource with your VideoMediaSource.
private MediaSource buildMediaSourceWithAds(Uri uri) {
DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(this,
Util.getUserAgent(this, getString(R.string.app_name)), BANDWIDTH_METER);
MediaSource videoSource = new ExtractorMediaSource.Factory(dataSourceFactory).createMediaSource(uri);
return new AdsMediaSource(
videoSource,
dataSourceFactory,
imaAdsLoader,
mPlayerView.getOverlayFrameLayout());
}