Exoplayer 2:从 cacheDir 播放 mp4
Exoplayer 2: Playing mp4 from cacheDir
我正在从服务器下载 mp4 到 cacheDir。稍后我想使用 Exoplayer 播放此视频。但是我做不到。
我在玩服务器时使用了以下内容Url:(效果很好)
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);
试试这个代码:
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 cacheDataSourceFactory = new CacheDataSourceFactory(cache, factory);
MediaSource audioSource = new ExtractorMediaSource(fileDataSource.getUri(),
cacheDataSourceFactory, new DefaultExtractorsFactory(), null, null);
我正在从服务器下载 mp4 到 cacheDir。稍后我想使用 Exoplayer 播放此视频。但是我做不到。
我在玩服务器时使用了以下内容Url:(效果很好)
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);
试试这个代码:
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 cacheDataSourceFactory = new CacheDataSourceFactory(cache, factory);
MediaSource audioSource = new ExtractorMediaSource(fileDataSource.getUri(),
cacheDataSourceFactory, new DefaultExtractorsFactory(), null, null);