SoFunction
Updated on 2025-04-10

Detailed explanation of Android VideoCache video caching method

Detailed explanation of Android VideoCache video caching method

When video playback is encountered in the project, the network url needs to be loaded. It is impossible to load the network every time. Of course, we need to use our cache mechanism.

AndroidVideoCache

AndroidVideoCache is a video/audio cache library that uses local proxy to achieve lower-level playback, which is very simple to use.

HttpProxyCacheServer is the main class and is a proxy server. It can configure the number of cached files, the size of cached files, the directory of cached files, and the cached file naming algorithm. The file cache is all based on the LRU algorithm and is configured using Builder:

//Configure cache directorypublic Builder cacheDirectory(File file);

//Configure cache file naming rulespublic Builder fileNameGenerator(FileNameGenerator fileNameGenerator) ;

//Configure cache file sizepublic Builder maxCacheSize(long maxSize) ;

//Configure the number of cached filespublic Builder maxCacheFilesCount(int count) ;

It is recommended to store HttpProxyCacheServer in Application in single column mode:

public class App extends Application {

  private HttpProxyCacheServer proxy;

  public static HttpProxyCacheServer getProxy(Context context) {
    App app = (App) ();
    return  == null ? ( = ()) : ;
  }

  private HttpProxyCacheServer newProxy() {
    return new HttpProxyCacheServer(this);
  }
}

Calling is very convenient, you only need to add one line of code:

@Override
protected void onCreate(Bundle savedInstanceState) {
(savedInstanceState);

  HttpProxyCacheServer proxy = getProxy();
  String proxyUrl = (VIDEO_URL);
  (proxyUrl);
}

private HttpProxyCacheServer getProxy() {
  return (getApplicationContext());
}

When the video is loaded, you need to determine whether it is cached, and do some operations such as hiding the buffer progress bar.

The above is a detailed explanation of how to use Android VideoCache. There are many articles on Android development on this website. I hope you search and read it. I hope you can help you. Thank you for your support for this website!