本文介绍了Exoplayer 2:从 cacheDir 播放 mp4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在将 mp4 从服务器下载到 cacheDir.后来我想用 Exoplayer 播放这个视频.但我做不到.
I am downloading mp4 from server to cacheDir. Later I want to play this video using Exoplayer. But I am unable to do so.
我使用以下播放服务器网址:(效果很好)
I used following for playing server Url: (It worked fine)
extractorsFactory = new DefaultExtractorsFactory();
MediaSource mediaSource = new ExtractorMediaSource(Uri.parse(videoURL),
cacheDataourceFactory, extractorsFactory, null, null);
来自 cacheDir 的本地 url:(不工作)
Local url from cacheDir: (not working)
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);
推荐答案
试试这个代码:
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);
这篇关于Exoplayer 2:从 cacheDir 播放 mp4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!