How to deal with ExoPlayer. Part-1📹

Ali Azaz Alam
AndroidPub
Published in
3 min readApr 3, 2019

Simple steps by which we can easily achieve the player view in the name of EXOPLAYER…

Few days ago, I was working on my project in which I want to play bunch of videos that’s exist in SD-Card storage and in raw folder. So, I implemented android default VideoView connected with MediaController and its worked fine. After then I realized that working on video player become cliché that’s only copy paste some code and got simple video player. So, I started exploring some different way to achieve either scenario like playing Audio or Video from same player and also have the customization functionality. Searching …. Then I got ExoPlayer.

This document will reveal the basis use of ExoPlayer in convenient way..

What’s an ExoPlayer is?

Refer to official website for more demonstration on ExoPlayer:

ExoPlayer is an application level media player for Android. It provides an alternative to Android’s MediaPlayer API for playing audio and video both locally and over the Internet. ExoPlayer supports features not currently supported by Android’s MediaPlayer API, including DASH and SmoothStreaming adaptive playbacks. Unlike the MediaPlayer API, ExoPlayer is easy to customize and extend, and can be updated through Play Store application updates.

Getting Started

Firstly, accommodate the following code by altering the repository under allprojects root in project.gradle file:

allprojects {
repositories {
// Add these lines
google()
jcenter()
}
}

Secondly, enable JAVA_8 support in app.gradle file under android root:

android {
………………………………
………………………………

/*Add Compile options in following block*/
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

}

Thirdly, incorporate exoplayer dependency in same file i.e app.gradle:

//Current version is 2.9.6
implementation 'com.google.android.exoplayer:exoplayer:2.X.X'

Congoo!!👌 we done the basis settings of ExoPlayer.

Implementation

Let’s maintain the code for playing video in ExoPalyer view.

XML

<com.google.android.exoplayer2.ui.PlayerView
android:id="@+id/player_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:repeat_toggle_modes="all|one"
/>

We defined the playerview in activity xml. Now, move forward to activity java file and do some code by creating exoplayer instance and some more stuff requires by the ExoPlayer View. Here is the code snippet 💥

// Binding xml view
PlayerView playerView = findViewById(R.id.player_view);
// Setup Exoplayer instance
SimpleExoPlayer exoPlayerInstance = ExoPlayerFactory.newSimpleInstance(this);
// Produces DataSource instances through which media data is loaded.
DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(this, Util.getUserAgent(this, "simpleExoPlayer"));

Then define the media source and attach it to the PlayerView

//Getting media from raw resource
MediaSource firstSource = new ExtractorMediaSource.Factory(dataSourceFactory).createMediaSource(RawResourceDataSource.buildRawResourceUri(R.raw.landscape));
//Prepare the exoPlayerInstance with the source
exoPlayerInstance.prepare(firstSource);

Finally attach the mediasource to PlayerView instance

playerView.setPlayer(exoPlayerInstance);

Yahh hooo 😎 our ExoPlayer playing video done. Run the app on device🤞

Fork the implementation on GITHUB

🤨Some more working with Exo

Access Videos from Phone storage

If the video is available in SD-Card storage then you can access it through this method.

// Getting media from sdcard storage
MediaSource secondSource = new ExtractorMediaSource.Factory(dataSourceFactory).createMediaSource(Uri.parse(Directory + File.separator + "landscape2"));

Directory — define the folder where the video is located

Directory = Environment.getExternalStorageDirectory() + File.separator + "EXOPLAYER-SAMPLE";

For playing more than one video define in ConcatenatingMediaSource

ConcatenatingMediaSource concatenatedSource = new ConcatenatingMediaSource(firstSource, secondSource);exoPlayerInstance.prepare(concatenatedSource);

You can do a lot of stuff by customizing the ExoPlayer on basis of your requirement.

Let’s do some more working in ExoPlayer but in Part-2 of ExoPlayer…

How to deal with ExoPlayer. Part-2

Thanks for spending your precious time in reading this article. It’s my first article on medium. If you liked it then Claps 👏 multiple times to say Thanks and helps others by referring it.

--

--

Ali Azaz Alam
AndroidPub

@AndroidAppsDeveloper, @OpensourceContributor, @Writer @Researcher