我正在使用SimpleCache存储通过ExoPlayer的DownloadService下载的Dash块,这些块存储在文件夹DOWNLOAD_CONTENT_DIRECTORY中。我的DownloadManager还将DownloadActions保存在文件夹DOWNLOAD_ACTION_FILE中。我想从磁盘上清除所有与下载相关的文件。我能想到的最简单的解决方案是直接删除存储在这些目录中的所有文件,但是我不知道这样做是否是正确的方法,而且不会损害ExoPlayer中的其他功能。我认为的另一种方法是,对于我拥有的每个下载都调用DownloadService.startWithAction(removeDownloadAction),但是在这种情况下,我想不加选择地删除所有下载。
public Cache getDownloadCache(Context context) {
if (downloadCache == null) {
File downloadContentDirectory = new File(getDownloadDirectory(context), DOWNLOAD_CONTENT_DIRECTORY);
downloadCache = new SimpleCache(downloadContentDirectory, new NoOpCacheEvictor());
}
return downloadCache;
}
public File getDownloadDirectory(Context context) {
File downloadDirectory = context.getExternalFilesDir(null);
if (downloadDirectory == null) {
downloadDirectory = context.getFilesDir();
}
return downloadDirectory;
}
这就是我创建DownlodManager的方式
private synchronized void initDownloadManager() {
if (downloadManager == null) {
DownloaderConstructorHelper downloaderConstructorHelper =
new DownloaderConstructorHelper(getDownloadCache(this), DataSourceUtil.buildHttpDataSourceFactory(this));
downloadManager =
new DownloadManager(
downloaderConstructorHelper,
MAX_SIMULTANEOUS_DOWNLOADS,
DownloadManager.DEFAULT_MIN_RETRY_COUNT,
new File(getDownloadDirectory(this), DOWNLOAD_ACTION_FILE));
}
}