ExoPlayer:Uri:无法加载本地媒体Android

时间:2017-09-02 13:40:35

标签: android exoplayer exoplayer2.x

我正在尝试将手机媒体播放到ExoPlayer中。 我从Environment.getExternalStorageDirectory().getAbsoluteFile();

获取路径

每当我尝试播放媒体时,我都会收到此错误 -

  

来源错误    com.google.android.exoplayer2.upstream.HttpDataSource $ HttpDataSourceException:   无法连接到/storage/emulated/0/Download/big_buck_bunny_720p_1mb.mp4

  

引起:java.net.MalformedURLException:no protocol:/storage/emulated/0/Download/big_buck_bunny_720p_1mb.mp4

我在这里通过Uri MediaSource mediaSource = buildMediaSource(Uri.parse(link));

此方法正在使用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);
}

这就是我获取链接的方式

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

1 个答案:

答案 0 :(得分:4)

使用FileDataSource。以下代码段从资产uri(如file:///android_asset/video.mp4或其他文件uris)创建mp4的MediaSource:

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