ExoPlayer is an Android Application level Media Player by Google. It is used for playing media(both audio and video) from local or remote. In this article we will discuss how to play or stream remote video using ExoPlayer.
LIBRARY SET UP for ExoPlayer
First of all, we need to create a new project(ExoPlayerSample here in this article) and then move to MainActivity.java as usual. Before we start implementation we need to add dependency for ExoPlayer.
So, now we move to project-level build.gradle file and include Google and JCenter repositories. It will look like below
1 2 3 4 5 6 7 8 9 |
repositories { //---------- google() jcenter() } |
Next, we need to add library dependency in app level build.gradle file.
1 2 3 4 5 6 7 8 |
dependencies { //-------------- implementation 'com.google.android.exoplayer:exoplayer:2.9.6' } |
In above section we have used version 2.9.6. But you should check for latest versions while implementing.
Another thing we need to add is INTERNET permission in AndroidManifest.xml. So the Manifest file will look like below
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.digitstory.exoplayersmaple"> <uses-permission android:name="android.permission.INTERNET"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity" android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> |
DESIGN
ExoPlayer comes with a default design but it has that much flexibility to add own custom design by maintaining some of it’s default view-ids for same code(Java) of implementation as with default design. But before that we will add view in MainActivity’s design file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <com.google.android.exoplayer2.ui.SimpleExoPlayerView android:id="@+id/playerView" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#000000" /> </android.support.constraint.ConstraintLayout> |
ExoPlayer IMPLEMENTATION
Now it’s time to implement the ExoPlayer in activity. The very first step is to initialise the player. Here we go
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
/** * Method to initialize player */ private void initializePlayer(){ try { if(player==null) { playerView.setPlayer(player); Uri uri = Uri.parse(VIDEO_DASH_URL); //Building data source DashMediaSource dashMediaSource = new DashMediaSource(uri, mediaDataSourceFactory, new DefaultDashChunkSource.Factory(mediaDataSourceFactory), null, null); //Setting Track selector trackSelector = new DefaultTrackSelector(new AdaptiveTrackSelection.Factory(bandwidthMeter)); //Creating exo-player instance player = ExoPlayerFactory.newSimpleInstance(this, trackSelector); playerView.setPlayer(player); //Performing play event when source is ready to play player.setPlayWhenReady(true); player.prepare(dashMediaSource); } player.seekTo(SEEK_POSITION); } catch (Exception e) { e.printStackTrace(); } } |
In above piece of code we have initialised ExoPlayer using DashMediaSource. DashMediaSource is responsible for playing the manifest at a given Uri which can be static or dynamic. Here SEEK_POSITION is the player’s last played position which is required to store for orientation change, minimising screen etc.
Now we will learn how to release the player while destroying or stopping Activity. This is simplified by ExoPlayer’s release() method which is needed to call where it is required.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
/** * Method to release Video player's when exiting form this activity */ private void releasePlayer() { try { if (player != null) { SEEK_POSITION = player.getCurrentPosition(); player.release(); player = null; trackSelector = null; } } catch (Exception e) { e.printStackTrace(); } } |
The above method can be called in onStop() or onDestroy() methods.
FULL ACTIVITY SOURCE
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
package com.digitstory.exoplayersmaple; import android.app.Activity; import android.net.Uri; import android.os.Bundle; import com.google.android.exoplayer2.ExoPlayerFactory; import com.google.android.exoplayer2.SimpleExoPlayer; import com.google.android.exoplayer2.source.dash.DashMediaSource; import com.google.android.exoplayer2.source.dash.DefaultDashChunkSource; import com.google.android.exoplayer2.trackselection.AdaptiveTrackSelection; import com.google.android.exoplayer2.trackselection.DefaultTrackSelector; import com.google.android.exoplayer2.trackselection.TrackSelector; import com.google.android.exoplayer2.ui.SimpleExoPlayerView; import com.google.android.exoplayer2.upstream.BandwidthMeter; import com.google.android.exoplayer2.upstream.DataSource; import com.google.android.exoplayer2.upstream.DefaultBandwidthMeter; import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory; import com.google.android.exoplayer2.util.Util; public class MainActivity extends Activity { SimpleExoPlayer player; SimpleExoPlayerView playerView; private BandwidthMeter bandwidthMeter; private DataSource.Factory mediaDataSourceFactory; private TrackSelector trackSelector; //Sample URL to stream private static final String VIDEO_DASH_URL = "https://bitmovin-a.akamaihd.net/content/playhouse-vr/mpds/105560.mpd"; private static long SEEK_POSITION = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); playerView = findViewById(R.id.playerView); bandwidthMeter = new DefaultBandwidthMeter(); mediaDataSourceFactory = new DefaultDataSourceFactory(this, Util.getUserAgent(this, "ExoPlayerSample")); } @Override protected void onStart() { super.onStart(); initializePlayer(); } /** * Method to initialize player */ private void initializePlayer() { try { if (player == null) { playerView.setPlayer(player); Uri uri = Uri.parse(VIDEO_DASH_URL); //Building data source DashMediaSource dashMediaSource = new DashMediaSource(uri, mediaDataSourceFactory, new DefaultDashChunkSource.Factory(mediaDataSourceFactory), null, null); //Setting Track selector trackSelector = new DefaultTrackSelector(new AdaptiveTrackSelection.Factory(bandwidthMeter)); //Creating exo-player instance player = ExoPlayerFactory.newSimpleInstance(this, trackSelector); playerView.setPlayer(player); //Performing play event when source is ready to play player.setPlayWhenReady(true); player.prepare(dashMediaSource); } player.seekTo(SEEK_POSITION); } catch (Exception e) { e.printStackTrace(); } } /** * Method to release Video player's when exiting form this activity */ private void releasePlayer() { try { if (player != null) { SEEK_POSITION = player.getCurrentPosition(); player.release(); player = null; trackSelector = null; } } catch (Exception e) { e.printStackTrace(); } } @Override protected void onPause() { super.onPause(); releasePlayer(); } @Override protected void onStop() { super.onStop(); releasePlayer(); } @Override protected void onDestroy() { super.onDestroy(); releasePlayer(); } } |
That was a small POC for streaming video using ExoPlayer in Android. You can find full source code from GitHub. We can do much more things with ExoPlayer like
- Play media using DRM
- Play local media
- Stream live video
- Customise player’s basic design with additional functionalities like minimise, maximise, skip etc.
Source : Github Documentation by Google
If you like this article spread it to inspire us.
Suggested Read : Install Referrer for Android using Google Play Campaign Measurement