1

我正在将 mp4 从服务器下载到 cacheDir。后来我想用 Exoplayer 播放这个视频。但我无法这样做。

我使用以下播放服务器网址:(效果很好)

extractorsFactory = new DefaultExtractorsFactory();
MediaSource mediaSource = new ExtractorMediaSource(Uri.parse(videoURL),
    cacheDataourceFactory, extractorsFactory, null, null);

来自 cacheDir 的本地 url:(不工作)

File file = new File(context.getApplicationContext().getCacheDir(), "/MP4/test.mp4");
extractorsFactory = new DefaultExtractorsFactory();
MediaSource mediaSource = new ExtractorMediaSource(Uri.parse(file.toString(), 
    cacheDataourceFactory, extractorsFactory, null, null);
4

1 回答 1

3

试试这个代码:

    File file = new File(context.getApplicationContext().getCacheDir(), "/MP4/test.mp4");        
    Uri uri = Uri.fromFile(file);      
    DataSpec dataSpec = new DataSpec(uri);
    final FileDataSource fileDataSource = new FileDataSource();
    try {
        fileDataSource.open(dataSpec);
    } catch (FileDataSource.FileDataSourceException e) {
        e.printStackTrace();
    }

    DataSource.Factory factory = new DataSource.Factory() {
        @Override
        public DataSource createDataSource() {
            return fileDataSource;
        }
    };
    CacheDataSourceFactory cacheDataourceFactory= new CacheDataourceFactory(cache, factory);
    MediaSource audioSource = new ExtractorMediaSource(fileDataSource.getUri(),
            cacheDataourceFactory, new DefaultExtractorsFactory(), null, null);
于 2017-08-21T16:41:52.060 回答