3

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;
}
gmetax
  • 3,853
  • 2
  • 31
  • 45
Avinash Saran
  • 144
  • 1
  • 9
  • Please provide a [mcve] demonstrating how you are creating the `Uri` and giving it to ExoPlayer. – CommonsWare Sep 02 '17 at 13:42
  • @CommonsWare I have edited the post – Avinash Saran Sep 02 '17 at 13:50
  • 1
    Stop using `Uri.parse()`. Use `Uri.fromFile()`. – CommonsWare Sep 02 '17 at 13:56
  • @CommonsWare after using this- Uri.fromFile(new File(link)); My error is : java.lang.ClassCastException: sun.net.www.protocol.file.FileURLConnection cannot be cast to java.net.HttpURLConnection -also- Unexpected exception loading stream java.lang.ClassCastException: sun.net.www.protocol.file.FileURLConnection cannot be cast to java.net.HttpURLConnection – Avinash Saran Sep 02 '17 at 14:01
  • 1
    Perhaps stop using `DefaultHttpDataSourceFactory`, since that would appear to be tied to HTTP, and your data source is not using HTTP. – CommonsWare Sep 02 '17 at 14:03
  • @CommonsWare Thank You very much! Problem is solved – Avinash Saran Sep 02 '17 at 14:40

1 Answers1

6

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);
  }
marcbaechinger
  • 2,759
  • 18
  • 21