How to get local video Uri for ExoPlayer 2.x

wilkas picture wilkas · Oct 27, 2016 · Viewed 7.7k times · Source

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?

Answer

Ali Azaz Alam picture Ali Azaz Alam · Mar 13, 2019

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);