- 
                Notifications
    You must be signed in to change notification settings 
- Fork 6.3k
Quickstart with Exoplayer
Exoplayer is an Advanced Video Library by Google that is aimed to be more customizable and flexible than the traditional video_view and MediaPlayer approach. Read more about it here.
In your gradle files, add these lines:
//project's build.gradle
repositories {
    google()
    jcenter()
}
//app/build.gradle
android {
    ...
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}
dependencies {
    ...
    implementation 'com.google.android.exoplayer:exoplayer:2.10.5' //current version as of oct,2019
    //there is also an option to import only specific modules
}The basic steps for playing a video(that is available on some server on the internet) via Exoplayer are:
- add the <player_view>in xml and create its instance: This will be the view on which the video will be rendered.
- create a Playerinstance : This will be the main Handler behind all the streaming , decoding and rendering of bitstreams
- create a MediaSourceInstance: This is the Class associated with the Uri of the content you intend to play
- attach playerView to player
- start video playback via absPlayerInternal.setPlayWhenReady(true);:this will automatically start playing video as soon as the video is available.( To control playback manually, simply passfalse, the playback will then only start when user presses theplaybutton .
In your activity or framgment's xml file, add these lines:
 <com.google.android.exoplayer2.ui.PlayerView
        android:id="@+id/pv_main"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        />
<!-- there are many customization attributes available like app:auto_show="true" or app:ad_marker_color="@color/colorAccent", etc which can be either used via xml or from java -->In your activity or fragment's java file, add these lines:
     SimpleExoPlayer absPlayerInternal;PlayerView pvMain;
     protected void onCreate(Bundle savedInstanceState) {
        ...
        
        String CONTENT_URL = "https://www.radiantmediaplayer.com/media/bbb-360p.mp4";
        
        int appNameStringRes = R.string.app_name;
        pvMain = findViewById(R.id.pv_main); // creating player view
        
        TrackSelector trackSelectorDef = new DefaultTrackSelector();
        absPlayerInternal = ExoPlayerFactory.newSimpleInstance(this, trackSelectorDef); //creating a player instance
        
        String userAgent = Util.getUserAgent(this, this.getString(appNameRes));
        DefaultDataSourceFactory defdataSourceFactory = new DefaultDataSourceFactory(this,userAgent);
        Uri uriOfContentUrl = Uri.parse(CONTENT_URL);
        MediaSource mediaSource = new ProgressiveMediaSource.Factory(defdataSourceFactory).createMediaSource(uriOfContentUrl);  // creating a media source
        absPlayerInternal.prepare(mediaSource);
        absPlayerInternal.setPlayWhenReady(true); // start loading video and play it at the moment a chunk of it is available offline
        pvMain.setPlayer(absPlayerInternal); // attach surface to the view
    }    private void pausePlayer(SimpleExoPlayer player) {
        if (player != null) {
            player.setPlayWhenReady(false);
        }
    }For resuming the video:
    private void playPlayer(SimpleExoPlayer player) {
        if (player != null) {
            player.setPlayWhenReady(true);
        }
    }For stopping the video:
    private void stopPlayer(){
        pv.setPlayer(null);
        absPlayer.release();
        absPlayer = null;
    }For seeking the video to some point:
    private void seekTo(SimpleExoPlayer player, long positionInMS) {
        if (player != null) {
            player.seekTo(positionInMS);
        }
    }Extras
- 
To silence video's volume, simply do : absPlayerInternal.setVolume(0f)
- 
The <PlayerView>has a built in Video controller that can be toggled usingpvMain.setUseController(/*true or false*/)and other functions. But if you want, you can also add an external exoplayer's controller view called<PlayerControlView>.
- 
Exoplayer has many useful extensions, some of which are mentioned here . 
Created by CodePath with much help from the community. Contributed content licensed under cc-wiki with attribution required. You are free to remix and reuse, as long as you attribute and use a similar license.
Finding these guides helpful?
We need help from the broader community to improve these guides, add new topics and keep the topics up-to-date. See our contribution guidelines here and our topic issues list for great ways to help out.
Check these same guides through our standalone viewer for a better browsing experience and an improved search. Follow us on twitter @codepath for access to more useful Android development resources.
