ExoPlayer : Uri : Not able to load local media Android

4.2k views Asked by At

I am trying to play media from mobile phone into ExoPlayer. I am getting the path from Environment.getExternalStorageDirectory().getAbsoluteFile();

Whenever I try to play media- I got this error-

Source error com.google.android.exoplayer2.upstream.HttpDataSource$HttpDataSourceException: Unable to connect to /storage/emulated/0/Download/big_buck_bunny_720p_1mb.mp4

also,

Caused by: java.net.MalformedURLException: no protocol: /storage/emulated/0/Download/big_buck_bunny_720p_1mb.mp4

I am passing Uri here MediaSource mediaSource = buildMediaSource(Uri.parse(link));

This method is using the Uri

private MediaSource buildMediaSource(Uri uri) {
    DataSource.Factory dataSourceFactory = new 
DefaultHttpDataSourceFactory("ua", BANDWIDTH_METER);
    DashChunkSource.Factory dashChunkSourceFactory = new 
DefaultDashChunkSource.Factory(dataSourceFactory);
    return new DashMediaSource(uri, dataSourceFactory, 
dashChunkSourceFactory, null, null);
}

This is how i am getting the link

arrayList1 = video(Environment.getExternalStorageDirectory().getAbsoluteFile());
protected ArrayList<File> video(File file) {
    File[] filename = file.listFiles();
    ArrayList<File>  arrayList2 = new ArrayList<File>();
    try {
        for (File file1 : filename) {
            if (file1.isDirectory()) {
                arrayList2.addAll(video(file1));
            } else {
                if (file1.getName().endsWith(".mp4")) {
                    arrayList2.add(file1);
                }
            }
        }
    }catch (Exception e){
    }
    return arrayList2;
}
2

There are 2 answers

0
marcbaechinger On

Use a FileDataSource. The following snippet creates a MediaSource for mp4 from an asset uri like file:///android_asset/video.mp4 or other file uris:

  private MediaSource buildMediaSource(Uri uri) {
    DataSource.Factory dataSourceFactory = new FileDataSourceFactory();
    return new ExtractorMediaSource(uri, dataSourceFactory, 
        new DefaultExtractorsFactory(), null, null);
  }
0
MEGHA RAMOLIYA On

In my case .mp4 file not support so, first i had tried to put local .mp4 file then check but exoplayer not support. I have call .m3u8 url and it work very fine.

https://demo.unified-streaming.com/k8s/features/stable/video/tears-of-steel/tears-of-steel.ism/.m3u8