How to get local video Uri for ExoPlayer 2.x
Asked Answered
C

7

12

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?

Cressida answered 27/10, 2016 at 4:15 Comment(1)
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.Cressida
C
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.

Cressida answered 28/10, 2016 at 5:23 Comment(3)
Could you elaborate on the solution you came up with?Corinnacorinne
As answer says, videos must reside in special folder "Assets". To add this folder, see #26707343Cressida
You can check my comment that below to this post. Hope I'll create the tutorial on exo in maybe some days.Absentminded
T
13

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
}
Turne answered 26/10, 2020 at 13:17 Comment(0)
A
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);
Absentminded answered 13/3, 2019 at 12:17 Comment(0)
C
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.

Cressida answered 28/10, 2016 at 5:23 Comment(3)
Could you elaborate on the solution you came up with?Corinnacorinne
As answer says, videos must reside in special folder "Assets". To add this folder, see #26707343Cressida
You can check my comment that below to this post. Hope I'll create the tutorial on exo in maybe some days.Absentminded
C
2

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()
Corbicula answered 13/4, 2023 at 4:59 Comment(0)
A
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();
}

}

Adolphus answered 5/5, 2020 at 6:48 Comment(3)
update yourself exoplayer 2.11.7 has released and your still with 2.8.*Spitter
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.Adolphus
But i was unable to play MKV video with your code and also tried with latest version still i was unable to play. https://mcmap.net/q/910477/-unable-to-play-mkv-matroska-video-with-exoplayer-2-11/3836908 lie this questionSpitter
A
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

Auntie answered 30/7, 2020 at 11:1 Comment(0)
J
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())
Jestinejesting answered 13/8, 2020 at 18:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.