Android Exoplayer – How to Play Videos Tutorial

Hi Friends,
In this post let’s see how we can play videos in Android using Exoplayer.

We require to play audio or videos in our application at different times to cater different needs and activities. There are several different ways to play audio/videos in Android ofcourse. We will see How we can use Android Exoplayer to play videos today?. So Let’s start. Download of source code is available too at end of the post.

Android Exoplayer Videos Tutorial:

DOWNLOAD SOURCE CODE.

In your Android project implement Android Exoplayer implementation in your project’s build.gradle

build.gradle implementation:

implementation 'com.google.android.exoplayer:exoplayer-core:2.7.3'
implementation 'com.google.android.exoplayer:exoplayer-dash:2.7.3'
implementation 'com.google.android.exoplayer:exoplayer-ui:2.7.3'

Now, let’s ask for the permission to one and only AndroidManifest file…shall we?

AndroidManifest.xml permissions:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

Create a video with name video1.mp4 in your phone’s video folder:

filepath: /storage/emulated/0/video/video1.mp4

You can add any video…….your call…..

Now open activity_main.xml file and edit it as below:

res > layout > activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="0dp">

<com.google.android.exoplayer2.ui.PlayerView
android:id="@+id/video_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true" />
</LinearLayout>

Now, open your MainActivity.java file as below:

MainActivity.java:

package com.parallel.videoplayer;

import android.net.Uri;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import com.google.android.exoplayer2.DefaultLoadControl;
import com.google.android.exoplayer2.DefaultRenderersFactory;
import com.google.android.exoplayer2.ExoPlayerFactory;
import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.extractor.DefaultExtractorsFactory;
import com.google.android.exoplayer2.source.ExtractorMediaSource;
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector;
import com.google.android.exoplayer2.ui.AspectRatioFrameLayout;
import com.google.android.exoplayer2.ui.PlayerView;
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;

import java.io.File;

public class MainActivity extends AppCompatActivity {

PlayerView playerView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

playerView = findViewById(R.id.video_view);
initializePlayer();
}

private void initializePlayer() {
SimpleExoPlayer player = ExoPlayerFactory.newSimpleInstance(
new DefaultRenderersFactory(this),
new DefaultTrackSelector(), new DefaultLoadControl());
String filePath = Environment.getExternalStorageDirectory() + File.separator +
"video" + File.separator + "video1.mp4";
Log.e("filepath", filePath);
Uri uri = Uri.parse(filePath);

ExtractorMediaSource audioSource = new ExtractorMediaSource(
uri,
new DefaultDataSourceFactory(this, "MyExoplayer"),
new DefaultExtractorsFactory(),
null,
null
);

player.prepare(audioSource);
playerView.setPlayer(player);
player.setPlayWhenReady(true);
}

}

Now you can run the app.

Android Exoplayer Videos Tutorial

P.S. : You will have to manually grant permission to our application from Android settings to make Android Exoplayer work on the app.

DOWNLOAD SOURCE CODE.

What is Android Exoplayer?

Exoplayer is an open source project which not a part of Android framework.

ExoPlayer’s standard audio and video components are built on Android’s MediaCodec API, which was released in Android 4.1 (API level 16). Because ExoPlayer is a library, you can easily take advantage of new features as they become available by updating your app.

Thanks For reading.

Android Exoplayer – Play Videos from raw folder

See Also:

Creating custom launcher in Android.

Creating Instagram like Login screen in Android.

Copying files from your phone to Windows in Android.


5 thoughts on “Android Exoplayer – How to Play Videos Tutorial”

  1. Hi there, I hope you can help me! What should I do if I receive an “ExoPlayerVideoHandler: This video is unavailable.” error message for some videos and not for other videos?

  2. Pingback: Download Source code No. 23 - ParallelCodes

  3. Pingback: Android How to play videos using VideoView - ParallelCodes

  4. Pingback: Android How to play videos from Storage and Filepath - ParallelCodes

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.