In this post we will see how to play videos from raw folder using Android Exoplayer library. In previous post we saw how to play videos from internal or gallery in android exoplayer. We will add a video in Android’s raw folder and play it in our application.
Copy any video of your choice in
res > raw > CopyInHere
The video name in this project I’m using is
res > raw > samplevideo.mp4
Edit your build.gradle file for app as below. We need to add exoplayer dependencies in our project.
build.gradle:
dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'com.android.support:appcompat-v7:28.0.0' implementation 'com.android.support.constraint:constraint-layout:1.1.3' 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' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' }
Edit your res > layout > activity_main.xml file as below. We will add Exoplayer video player in this file to play video.
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:focusable="true" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
Now edit your MainActivity.java file as below. To play video added into raw folder we will use Exoplayer’s resource builder. It will provide a URI for our video to Exoplayer. Like:
RawResourceDataSource.buildRawResourceUri(R.raw.samplevideo)
This will create a video URI and pass it on.
MainActivity.java:
package com.parallel.exoplayersample; import android.net.Uri; import android.os.Environment; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; 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 com.google.android.exoplayer2.upstream.RawResourceDataSource; 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 videoPath = RawResourceDataSource.buildRawResourceUri(R.raw.samplevideo).toString(); Uri uri = RawResourceDataSource.buildRawResourceUri(R.raw.samplevideo); ExtractorMediaSource audioSource = new ExtractorMediaSource( uri, new DefaultDataSourceFactory(this, "MyExoplayer"), new DefaultExtractorsFactory(), null, null ); player.prepare(audioSource); playerView.setPlayer(player); playerView.setResizeMode(AspectRatioFrameLayout.RESIZE_MODE_ZOOM); player.setPlayWhenReady(true); } }
Now let’s run our video player:
DOWNLOAD SOURCE CODE