Exoplayer 2:从cacheDir播放mp4

时间:2017-08-21 16:11:11

标签: android android-videoview exoplayer android-storage

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

我使用以下内容来播放服务器Url :(它工作正常)

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

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

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

1 个答案:

答案 0 :(得分:2)

试试这段代码:

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