清除exoplayer的缓存

时间:2018-10-14 07:45:07

标签: android caching exoplayer

我已将此link引用用于为Exo-Player创建CachedDataSource,并成功创建了它,但是不确定如何删除已加载视频的缓存?
这里有一个代码示例here ,但是我不确定如何实现它。任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:3)

如果您创建了一个缓存文件夹,如以下所述 https://exoplayer.dev/downloading-media.html 科特琳的例子

@Synchronized
fun getDownloadCache(): Cache {
    if (downloadCache == null) {
        val downloadContentDirectory = File(getDownloadDirectory(), DOWNLOAD_CONTENT_DIRECTORY)
        downloadCache = SimpleCache(downloadContentDirectory, NoOpCacheEvictor(), getDatabaseProvider())
    }
    return downloadCache as Cache
}

您只需在代码中的任何位置访问此缓存即可

fun clearCache() {
    application.getDownloadCache().release()
}

或使用以下命令清除单个键:

fun removeKeyFromCache(key: String) {
    CacheUtil.remove(application.getDownloadCache(), key)
}

应用程序是您的应用程序的实例,可以这样创建

lateinit var singleton: MyApp
open class MyApp: Application() {
  override fun onCreate() {
    super.onCreate()
    singleton = this
  }
}

并从任何地方访问它:

private val application: MyApp = singleton

答案 1 :(得分:0)

对我有用:

    public static void clearVideoCache(Context context){
        try {
            File dir = new File(context.getCacheDir(), cacheFolder);
            deleteDir(dir);
        } catch (Exception e) { e.printStackTrace();}
    }
    
    public static boolean deleteDir(File dir) {
        if (dir != null && dir.isDirectory()) {
            String[] children = dir.list();
            for (int i = 0; i < children.length; i++) {
                boolean success = deleteDir(new File(dir, children[i]));
                if (!success) {
                    return false;
                }
            }
            return dir.delete();
        } else if(dir!= null && dir.isFile()) {
            return dir.delete();
        } else {
            return false;
        }
    }

完整代码:

public class VideoCache {
    private static final String cacheFolder = "exoCache";
    private static SimpleCache sDownloadCache;

    public static SimpleCache getInstance(Context context) {
        if (sDownloadCache == null) sDownloadCache = new SimpleCache(new File(context.getCacheDir(), cacheFolder), new LeastRecentlyUsedCacheEvictor(2000 * 1024 * 1024), new ExoDatabaseProvider(context));
        return sDownloadCache;
    }

    public static void clearVideoCache(Context context){
        try {
            File dir = new File(context.getCacheDir(), cacheFolder);
            deleteDir(dir);
        } catch (Exception e) { e.printStackTrace();}
    }

    public static boolean deleteDir(File dir) {
        if (dir != null && dir.isDirectory()) {
            String[] children = dir.list();
            for (int i = 0; i < children.length; i++) {
                boolean success = deleteDir(new File(dir, children[i]));
                if (!success) {
                    return false;
                }
            }
            return dir.delete();
        } else if(dir!= null && dir.isFile()) {
            return dir.delete();
        } else {
            return false;
        }
    }

}

答案 2 :(得分:-1)

您可以通过访问保存高速缓存的目录来执行此操作,方法是在OnDestroy方法中进行操作。希望对您有帮助

JAVA

 File file = new File(mContext.cacheDir, "NameFolder")
 file.delete()

科特琳

  val file = File(mContext.getCacheDir(), "NameFolder")
  file.delete()