首页 > 解决方案 > 如何删除 ExoPlayer 中的所有下载和动作文件

问题描述

我正在使用 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));

    }
}

标签: androidexoplayer

解决方案


推荐阅读