Coder Social home page Coder Social logo

videocache's Introduction

VideoCache

Android视频缓存框架,避免多次或重复下载消耗更多的时间和流量。

特点

  • 流式传输期间缓存到磁盘
  • 离线使用缓存资源
  • 部分加载
  • 缓存限制(最大缓存大小,最大文件数)
  • 多个客户端访问同一缓存

说明

使用代理URL替代原始URL来开启URL,Android P及以上请关闭禁止明文通信

@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。例如,您可以将共享代理存储在您的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);
    }
}

磁盘缓存限制

默认情况下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();
}

为缓存文件提供名称

默认AndroidVideoCache使用视频 url 的 MD5 作为文件名。但在某些情况下 url 不稳定,它可能包含一些生成的部分(例如会话令牌)。在这种情况下,缓存机制将被破坏。要修复它,您必须提供自己的FileNameGenerator:

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

    //header key是否有效
    @Override
    public Boolean filter(String url, String key) {
        return null;
    }
}
private HttpProxyCacheServer newProxy() {
    return new HttpProxyCacheServer.Builder(this)
            .headerInjector(new UserAgentHeadersInjector())
            .build();
}

自定义网络视频加载

自定义视频下载

public class MySource implements Source {
    @Override
    public void open(long offset) throws ProxyCacheException {

    }

    @Override
    public long length() throws ProxyCacheException {
        return 0;
    }

    @Override
    public int read(byte[] buffer) throws ProxyCacheException {
        return 0;
    }

    @Override
    public void close() throws ProxyCacheException {

    }

    @Override
    public String getMime() throws ProxyCacheException {
        return null;
    }

    @Override
    public String getUrl() {
        return null;
    }
}

public class MySourceCreator implements SourceCreator<MySource> {
    @Override
    public MySource create(String url, SourceInfoStorage sourceInfoStorage, Map<String, String> headers) {
        return null;
    }

    @Override
    public MySource create(MySource source) {
        return null;
    }
}
private HttpProxyCacheServer newProxy() {
    return new HttpProxyCacheServer.Builder(this)
            .sourceCreator(new MySourceCreator())
            .build();
}

如何配置

将本仓库引入你的项目:

Step 1. 添加JitPack仓库到Build文件

合并以下代码到项目根目录下的build.gradle文件的repositories尾。

allprojects {
	repositories {
		...
		maven { url 'https://jitpack.io' }
	}
}

Step 2. 添加依赖

合并以下代码到需要使用的application Module的dependencies尾。

	dependencies {
	  ...
          compile 'com.iwdael:videocache:$version'
	}

感谢

Power by danikula/AndroidVideoCache

videocache's People

Contributors

iwdael avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.