Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get local video Uri for ExoPlayer 2.x

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?

like image 680
wilkas Avatar asked Oct 27 '16 04:10

wilkas


People also ask

How do I play the next ExoPlayer video on Android?

Show activity on this post. 3] Just check by clicking next button from the media controller if that works then you are done, now the videos will be played automatically once finished the current one.

How do I get ExoPlayer thumbnails on Android?

Add ImageView and Exoplayer in Framelayout. Show Image (That is knows as thumbnails) in imageView and on tap of ImageView Hide it and show ExoPlayer and play it.


1 Answers

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);
like image 56
Ali Azaz Alam Avatar answered Oct 16 '22 10:10

Ali Azaz Alam