【问题标题】:Clear cache of exoplayer清除exoplayer的缓存
【发布时间】:2019-03-18 22:14:14
【问题描述】:

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

【问题讨论】:

    标签: android caching exoplayer


    【解决方案1】:

    它对我有用:

        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】:

      如果您创建了一个缓存文件夹,如中所述 https://exoplayer.dev/downloading-media.html 以 kotlin 为例

      @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
      

      【讨论】:

      • 这对我很有帮助,谢谢,但是我对 key 是什么感到困惑,但我发现静态乐趣会在 CacheUtil 中生成密钥:CacheUtil.generateKey(Uri.parse("www.test.com/1.mp3"))
      【解决方案3】:

      您可以通过访问保存缓存的目录来执行此操作,即在 OnDestroy 方法中执行此操作。希望能帮到你

      JAVA

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

      科特林

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

      【讨论】:

        猜你喜欢
        • 2014-11-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-25
        • 2017-04-06
        • 1970-01-01
        • 2016-04-09
        • 2015-03-10
        相关资源
        最近更新 更多