我正在从服务器下载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);
答案 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);