Android視頻/音頻緩存框架AndroidVideoCache

AndroidVideoCache是一個視頻/音頻緩存庫,利用本地代理實現了邊下邊播,使用起來很是簡單。git

HttpProxyCacheServer是主要類,是一個代理服務器,能夠配置緩存文件的數量、緩存文件的大小、緩存文件的目錄和緩存文件命名算法,文件緩存均基於LRU算法,利用Builder來配置:github

//配置緩存目錄
public Builder cacheDirectory(File file);

//配置緩存文件命名規則
public Builder fileNameGenerator(FileNameGenerator fileNameGenerator) ;

//配置緩存文件大小
public Builder maxCacheSize(long maxSize) ;

//配置緩存文件數量
public Builder maxCacheFilesCount(int count) ;

建議以單列模式將HttpProxyCacheServer存放於Application中:算法

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);
    }
}

調用十分方便,只須要增長一行代碼:緩存

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

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

private HttpProxyCacheServer getProxy() {
    return App.getProxy(getApplicationContext());
}
相關文章
相關標籤/搜索