How to develop a video player using ExoPlayer

How to develop a video player using ExoPlayer

نتيجة بحث الصور عن ‪exoplayer‬‏

  • Why ExoPlayer؟
There are a lot of Media players which we can use, The most common one is "MediaPlayer" which supports a lot of common media formats but it\s hard to customize , edit or add items to it, And as it's not a library we cannot update or upgrade to other versions

- ExoPlayer is a library from google so we can determine which version to compile and it supports a large set of media formats and samples ،Easy to customize , edit or add new items also it supports high performance systems like  Dash , HLS
To read more about ExoPlayer and its supported formats check the next link


  • If you just want to make a youtube video player please check out the Youtube Api in the next link


Let's start

 first we should add library dependency in build.gradle app file

    compile 'com.google.android.exoplayer:exoplayer:r2.4.2'

This for the latest version

and add it to our xml
    <com.google.android.exoplayer2.ui.SimpleExoPlayerView
        android:id="@+id/playerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />


Now we write a  reference for our view and declare our data source which should be Uri

String mVideoUrl = "https://s3-us-west-2.amazonaws.com/askquran-media/video/Arabic_Efasy_001_001_007.mp4";
SimpleExoPlayerView playerView = (playerView) findViewById(R.id.playerView);
playerView.requestFocus();
initializePlayer(Uri.parse(mVideoUrl));
Now we will create an object from SimpleExoPlayer that's actually or player
private SimpleExoPlayer mExoPlayer;
Now we have to create a method for initialize and load our data source to the player and here we are

 // Initialize the exoPlayer
    private void initializePlayer(Uri mediaUri) {
        if (mExoPlayer == null) {
            TrackSelector trackSelector = new DefaultTrackSelector();
            LoadControl loadControl = new DefaultLoadControl();
            mExoPlayer = ExoPlayerFactory.newSimpleInstance(getContext(), trackSelector, loadControl);
            binding.playerView.setPlayer(mExoPlayer);            
            String userAgent = Util.getUserAgent(getContext(), "");
            MediaSource mediaSource = new ExtractorMediaSource(mediaUri, new DefaultDataSourceFactory(
                    getContext(), userAgent), new DefaultExtractorsFactory(), null, null);
            mExoPlayer.prepare(mediaSource);
            mExoPlayer.setPlayWhenReady(true);
        }
    }

the last line which is setPlayWhenReady if you want to play the media automatically when it's ready
we can pass false for this method anytime when we want to pause the player
mExoPlayer.setPlayWhenReady(false);
Now we should handle our resources
mean that when the user leave our application we should release the player as here i called that method releasePlayer and we should call it from onStop
private void releasePlayer() {
    if (mExoPlayer != null) {
        mExoPlayer.stop();
        mExoPlayer.release();
        mExoPlayer = null;
    }
}


  • you can save the last position before calling release and when you initialize the player again check that variable and seekTo that saved position to continue from the last point 
I hope it get's clear and sorry for my bad English ;D 
==