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

问题描述 投票:0回答:2

我正在尝试将手机中的媒体播放到 ExoPlayer 中。 我从

Environment.getExternalStorageDirectory().getAbsoluteFile();

获取路径

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

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

还有,

引起:java.net.MalformedURLException:无协议:/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;
}
android exoplayer exoplayer2.x
2个回答
6
投票

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

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

0
投票

就我而言,.mp4 文件不支持,所以首先我尝试放入本地 .mp4 文件,然后检查,但 exoplayer 不支持。我有电话 .m3u8 网址,它工作得很好。

https://demo.unified-streaming.com/k8s/features/stable/video/tears-of-steel/tears-of-steel.ism/.m3u8
© www.soinside.com 2019 - 2024. All rights reserved.