9

I have a dog.mp4 video file in res/raw folder, which I want to play with ExoPlayer. I'm trying to figure out how to get video Uri for this line of code from ExoPlayer developers guide (https://google.github.io/ExoPlayer/guide.html):

MediaSource videoSource = new ExtractorMediaSource(mp4VideoUri,
    dataSourceFactory, extractorsFactory, null, null);

To get it, I use this line:

Uri mp4VideoUri = Uri.parse("android.resources://"+getPackageName()+"/"+R.raw.dog);

Also tried this syntax: android.resource://[package]/[res type]/[res name]

But SimpleExoPlayerView stays black and I get following error:

com.google.android.exoplayer2.upstream.HttpDataSource$HttpDataSourceException: Unable to connect to android.resources://lt.wilkas.deleteexoplayer/2131099648

What am I doing wrong?

wilkas
  • 1,161
  • 1
  • 12
  • 34
  • I would also appreciate any simple tutorial how to play local video with ExoPlayer. Demo app on developers site is quite complicated for me to grasp everything. – wilkas Oct 27 '16 at 06:33

7 Answers7

9

Don't move your videos from raw to any another folder

Use this code to play video:

    PlayerView playerView = findViewById(R.id.player_view);

    SimpleExoPlayer player = ExoPlayerFactory.newSimpleInstance(this);

    // Bind the player to the view.
    playerView.setPlayer(player);

    // Produces DataSource instances through which media data is loaded.
    DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(this, Util.getUserAgent(this, "yourApplicationName"));

    // This is the MediaSource representing the media to be played.
    MediaSource firstSource = new ExtractorMediaSource.Factory(dataSourceFactory).createMediaSource(RawResourceDataSource.buildRawResourceUri(R.raw.dog));

    // Prepare the player with the source.
    player.prepare(firstSource);
Ali Azaz Alam
  • 1,782
  • 1
  • 16
  • 27
9

According to ExoPlayer 2.12 its even easier to use MediaItem. To create it we need Uri that could be generated from RawResourceDataSource.buildRawResourceUri. And that's it. Just set this MediaItem do prepare() and play when ready:

SimpleExoPlayer.Builder(view.context).build().apply {
    val uri = RawResourceDataSource.buildRawResourceUri(R.raw.video)
    setMediaItem(MediaItem.fromUri(uri))
    prepare()
    playWhenReady = true
}
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Dima S
  • 469
  • 5
  • 9
5

I've found out that res/raw folder cant be used to store local videos for ExoPlayer. They should be placed in assets folder.

wilkas
  • 1,161
  • 1
  • 12
  • 34
  • Could you elaborate on the solution you came up with? – RazorHead Nov 29 '16 at 09:00
  • 1
    As answer says, videos must reside in special folder "Assets". To add this folder, see http://stackoverflow.com/questions/26706843/adding-an-assets-folder-in-android-studio – wilkas Nov 29 '16 at 09:03
  • 1
    You can check my comment that below to this post. Hope I'll create the tutorial on exo in maybe some days. – Ali Azaz Alam Mar 14 '19 at 10:55
1

res/raw folder can be used to access local video file for ExoPlayer via it's URI. Here's the solution that will not give the Malformed URL exception and it works for me. You have to use RawResourceDataSource.buildRawResourceUri(R.raw.video) method of the RawResourceDataSource class. Keep in mind that i used the 2.8.0 version of ExoPlayer.

public class MainActivity extends AppCompatActivity {

PlayerView playerView;
SimpleExoPlayer simpleExoPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    playerView=findViewById(R.id.playerView);
}

@Override
protected void onStart() {
    simpleExoPlayer= ExoPlayerFactory.newSimpleInstance(this,new DefaultTrackSelector());
    DefaultDataSourceFactory defaultDataSourceFactory=new DefaultDataSourceFactory(this, Util.getUserAgent(this,"yourApplicationName"));
    simpleExoPlayer.setPlayWhenReady(true);
    ExtractorMediaSource extractorMediaSource=new ExtractorMediaSource.Factory(defaultDataSourceFactory).createMediaSource(RawResourceDataSource.buildRawResourceUri(R.raw.video));
    simpleExoPlayer.prepare(extractorMediaSource);
    playerView.setPlayer(simpleExoPlayer);

    super.onStart();
}

@Override
protected void onStop() {
    playerView.setPlayer(null);
    simpleExoPlayer.release();
    simpleExoPlayer=null;
    super.onStop();
}

}

Usama Mehmood
  • 95
  • 1
  • 3
  • 1
    update yourself exoplayer `2.11.7` has released and your still with `2.8.*` – sanoj lawrence Jul 28 '20 at 09:36
  • I absolutely know that. I am getting some issues with the later version and this version is getting my job done therefore i used this one. – Usama Mehmood Jul 31 '20 at 12:31
  • But i was unable to play `MKV` video with your code and also tried with latest version still i was unable to play. https://stackoverflow.com/q/63101825/3836908 lie this question – sanoj lawrence Aug 01 '20 at 10:12
0

here is the sample to load video/audio file from res/raw with ExoPlayer :

val player = ExoPlayerFactory.newSimpleInstance(context,DefaultTrackSelector())

val rawDataSource = RawResourceDataSource(context)

rawDataSource.open(DataSpec(RawResourceDataSource.buildRawResourceUri(R.raw.brown)))

val mediaSource = ExtractorMediaSource.Factory(DataSource.Factory { rawDataSource })
    .createMediaSource(rawDataSource.uri)

player.playWhenReady = true
player.prepare(mediaSource)

sample copied from

Mohad Hadi
  • 1,826
  • 3
  • 24
  • 39
0

This code works for me.

fun buildMediaSourceRaw() : MediaSource{
        val dataSourceFactory = DefaultDataSourceFactory(context, "exoPlayer")
        val uri = RawResourceDataSource.buildRawResourceUri(R.raw.my_video)
        return ProgressiveMediaSource.Factory(dataSourceFactory).createMediaSource(uri)
    }

Then

player?.prepare(buildMediaSourceRaw())
LuzLaura98
  • 41
  • 6
0

If you want to make it as MediaItem, this code work for me (kotlin)

val localMediaItem = MediaItem.fromUri(
        RawResourceDataSource.buildRawResourceUri(R.raw.your_video)
    )

then

player.setMediaItem(localMediaItem)
player.prepare()
Weka
  • 1