1. 程式人生 > >android視訊播放器快取

android視訊播放器快取

今天介紹一個視訊離線快取的框架,由於視訊播放的時候下載多次是沒有意義的。今天介紹的AndroidVideoCache支援VideoView/MediaPlayer, ExoPlayer等播放器實現離線快取功能。

主要特徵:

  • 流媒體磁碟快取;
  • 資源離線快取;
  • 區域性載入;
  • 快取限制 (最大快取大小, 最大檔案數);
  • 支援多客戶端.

注意,AndroidVideoCache只對媒體檔案使用直接url,它不支援任何流技術,如DASH,平滑流,HLS。

開始使用

增加依賴

dependencies {
    compile 'com.danikula:videocache:2.7.0'
}

並使用代理的url代替原來的url來新增快取:

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

    HttpProxyCacheServer proxy = getProxy();
    String proxyUrl = proxy.getProxyUrl(VIDEO_URL);
    videoView.setVideoPath(proxyUrl);
}

private HttpProxyCacheServer getProxy
() { // should return single instance of HttpProxyCacheServer shared for whole app. }

為了保證正常工作,你應該在整個應用程式中使用一個HttpProxyCacheServer的例項。

public class App extends Application {

    private HttpProxyCacheServer proxy;

    public static HttpProxyCacheServer getProxy(Context context) {
        App app = (App) context.getApplicationContext();
        return
app.proxy == null ? (app.proxy = app.newProxy()) : app.proxy; } private HttpProxyCacheServer newProxy() { return new HttpProxyCacheServer(this); } }

或者使用簡單工廠。更可取的方法是使用像Dagger這樣的依賴注入器。

方法

磁碟快取

預設情況下,HttpProxyCacheServer使用512Mb的快取檔案。你可以改變這個值:

private HttpProxyCacheServer newProxy() {
    return new HttpProxyCacheServer.Builder(this)
            .maxCacheSize(1024 * 1024 * 1024)       // 1 Gb for cache
            .build();
}

或者可以限制快取中的檔案總數:

private HttpProxyCacheServer newProxy() {
    return new HttpProxyCacheServer.Builder(this)
            .maxCacheFilesCount(20)
            .build();
}

甚至實施你自己的DiskUsage策略:

private HttpProxyCacheServer newProxy() {
    return new HttpProxyCacheServer.Builder(this)
            .diskUsage(new MyCoolDiskUsageStrategy())
            .build();
}

監聽快取進度

使用HttpProxyCacheServer。registerCacheListener(CacheListener listener)方法,可以用回撥onCacheAvailable(File cacheFile,String url,int percentsAvailable)來設定監聽器,以瞭解快取的進度。不要忘記在HttpProxyCacheServer的幫助下取消訂閱偵聽器。unregisterCacheListener(CacheListener listener)方法以避免記憶體洩漏。

使用HttpProxyCacheServer。isCached(String url)方法檢查url的內容是否完全快取到檔案中。

為快取的檔案提供名稱

預設情況下,AndroidVideoCache使用MD5的視訊url作為檔名。但在某些情況下,url不穩定,它可以包含一些生成的部分(例如會話標記)。在這種情況下,快取機制將被破壞。要修復它,您必須提供自己的檔案生成器:

public class MyFileNameGenerator implements FileNameGenerator {

    // Urls contain mutable parts (parameter 'sessionToken') and stable video's id (parameter 'videoId').
    // e. g. http://example.com?videoId=abcqaz&sessionToken=xyz987
    public String generate(String url) {
        Uri uri = Uri.parse(url);
        String videoId = uri.getQueryParameter("videoId");
        return videoId + ".mp4";
    }
}

...
HttpProxyCacheServer proxy = HttpProxyCacheServer.Builder(context)
    .fileNameGenerator(new MyFileNameGenerator())
    .build()

新增自定義http標頭

您可以在HeadersInjector的幫助下新增定製的標題:

public class UserAgentHeadersInjector implements HeaderInjector {

    @Override
    public Map<String, String> addHeaders(String url) {
        return Maps.newHashMap("User-Agent", "Cool app v1.1");
    }
}

private HttpProxyCacheServer newProxy() {
    return new HttpProxyCacheServer.Builder(this)
            .headerInjector(new UserAgentHeadersInjector())
            .build();
}

使用 exoPlayer

您可以使用exoPlayer和AndroidVideoCache。參見exoPlayer分支中的示例應用程式。註釋exoPlayer也支援快取。

例子

詳細使用方法見sample

專案地址