Java Code Examples for com.danikula.videocache.HttpProxyCacheServer

The following examples show how to use com.danikula.videocache.HttpProxyCacheServer. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: AndriodVideoCache   Source File: ProxyCacheTestUtils.java    License: Apache License 2.0 6 votes vote down vote up
public static Response readProxyResponse(HttpProxyCacheServer proxy, String url, int offset) throws IOException {
    String proxyUrl = proxy.getProxyUrl(url, false);
    if (!proxyUrl.startsWith("http://127.0.0.1")) {
        throw new IllegalStateException("Proxy url " + proxyUrl + " is not proxied! Original url is " + url);
    }
    URL proxiedUrl = new URL(proxyUrl);
    HttpURLConnection connection = (HttpURLConnection) proxiedUrl.openConnection();
    try {
        if (offset >= 0) {
            connection.setRequestProperty("Range", "bytes=" + offset + "-");
        }
        return new Response(connection);
    } finally {
        connection.disconnect();
    }
}
 
Example 2
Source Project: DKVideoPlayer   Source File: CacheActivity.java    License: Apache License 2.0 6 votes vote down vote up
@Override
    protected void initView() {
        super.initView();
        mVideoView = findViewById(R.id.video_view);
        HttpProxyCacheServer cacheServer = ProxyVideoCacheManager.getProxy(this);
        String proxyUrl = cacheServer.getProxyUrl(DataUtil.SAMPLE_URL);
        mVideoView.setUrl(proxyUrl);
        StandardVideoController controller = new StandardVideoController(this);
        controller.addDefaultControlComponent(getString(R.string.str_cache), false);
        mVideoView.setVideoController(controller);
        mVideoView.start();

        //删除url对应默认缓存文件
//        ProxyVideoCacheManager.clearDefaultCache(this, URL);
        //清除缓存文件中的所有缓存
//        ProxyVideoCacheManager.clearAllCache(this);
    }
 
Example 3
Source Project: AndroidVideoCache   Source File: ProxyCacheTestUtils.java    License: Apache License 2.0 6 votes vote down vote up
public static Response readProxyResponse(HttpProxyCacheServer proxy, String url, int offset) throws IOException {
    String proxyUrl = proxy.getProxyUrl(url, false);
    if (!proxyUrl.startsWith("http://127.0.0.1")) {
        throw new IllegalStateException("Proxy url " + proxyUrl + " is not proxied! Original url is " + url);
    }
    URL proxiedUrl = new URL(proxyUrl);
    HttpURLConnection connection = (HttpURLConnection) proxiedUrl.openConnection();
    try {
        if (offset >= 0) {
            connection.setRequestProperty("Range", "bytes=" + offset + "-");
        }
        return new Response(connection);
    } finally {
        connection.disconnect();
    }
}
 
Example 4
Source Project: Slide   Source File: Reddit.java    License: GNU General Public License v3.0 6 votes vote down vote up
@Override
public void onCreate() {
    super.onCreate();
    mApplication = this;
    //  LeakCanary.install(this);
    if (ProcessPhoenix.isPhoenixProcess(this)) {
        return;
    }

    proxy = new HttpProxyCacheServer.Builder(this).maxCacheSize(5 * 1024)
            .maxCacheFilesCount(20)
            .build();

    UpgradeUtil.upgrade(getApplicationContext());
    doMainStuff();
}
 
Example 5
Source Project: v9porn   Source File: AppDataManager.java    License: MIT License 5 votes vote down vote up
@Inject
AppDataManager(DbHelper mDbHelper, PreferencesHelper mPreferencesHelper, ApiHelper mApiHelper, HttpProxyCacheServer httpProxyCacheServer, CookieManager cookieManager, User user) {
    this.mDbHelper = mDbHelper;
    this.mPreferencesHelper = mPreferencesHelper;
    this.mApiHelper = mApiHelper;
    this.httpProxyCacheServer = httpProxyCacheServer;
    this.cookieManager = cookieManager;
    this.user = user;
}
 
Example 6
Source Project: v9porn   Source File: ApplicationModule.java    License: MIT License 5 votes vote down vote up
@Singleton
@Provides
static HttpProxyCacheServer providesHttpProxyCacheServer(@ApplicationContext Context context,HeaderInjector headerInjector) {
    return new HttpProxyCacheServer.Builder(context)
            // 1 Gb for cache
            .headerInjector(headerInjector)
            .maxCacheSize(AppCacheUtils.MAX_VIDEO_CACHE_SIZE)
            .cacheDirectory(AppCacheUtils.getVideoCacheDir(context))
            .fileNameGenerator(new VideoCacheFileNameGenerator())
            .build();
}
 
Example 7
Source Project: imsdk-android   Source File: HttpProxyUtil.java    License: MIT License 5 votes vote down vote up
private static HttpProxyCacheServer newProxy(Context context) {
    return new HttpProxyCacheServer.Builder(context)
            .fileNameGenerator(new FileNameGenerator() {
                @Override
                public String generate(String url) {
                    return  findRealUrl(url);
                }
            })
            .build();
}
 
Example 8
Source Project: v9porn   Source File: AppDataManager.java    License: MIT License 5 votes vote down vote up
@Inject
AppDataManager(DbHelper mDbHelper, PreferencesHelper mPreferencesHelper, ApiHelper mApiHelper, HttpProxyCacheServer httpProxyCacheServer, CookieManager cookieManager, User user) {
    this.mDbHelper = mDbHelper;
    this.mPreferencesHelper = mPreferencesHelper;
    this.mApiHelper = mApiHelper;
    this.httpProxyCacheServer = httpProxyCacheServer;
    this.cookieManager = cookieManager;
    this.user = user;
}
 
Example 9
Source Project: v9porn   Source File: ApplicationModule.java    License: MIT License 5 votes vote down vote up
@Singleton
@Provides
static HttpProxyCacheServer providesHttpProxyCacheServer(@ApplicationContext Context context,HeaderInjector headerInjector) {
    return new HttpProxyCacheServer.Builder(context)
            // 1 Gb for cache
            .headerInjector(headerInjector)
            .maxCacheSize(AppCacheUtils.MAX_VIDEO_CACHE_SIZE)
            .cacheDirectory(AppCacheUtils.getVideoCacheDir(context))
            .fileNameGenerator(new VideoCacheFileNameGenerator())
            .build();
}
 
Example 10
Source Project: AndriodVideoCache   Source File: VideoFragment.java    License: Apache License 2.0 5 votes vote down vote up
private void checkCachedState() {
    HttpProxyCacheServer proxy = App.getProxy(getActivity());
    boolean fullyCached = proxy.isCached(url);
    setCachedState(fullyCached);
    if (fullyCached) {
        progressBar.setSecondaryProgress(100);
    }
}
 
Example 11
Source Project: AndriodVideoCache   Source File: VideoFragment.java    License: Apache License 2.0 5 votes vote down vote up
private void startVideo() {
    HttpProxyCacheServer proxy = App.getProxy(getActivity());
    proxy.registerCacheListener(this, url);
    String proxyUrl = proxy.getProxyUrl(url);
    Log.d(LOG_TAG, "Use proxy url " + proxyUrl + " instead of original url " + url);
    videoView.setVideoPath(proxyUrl);
    videoView.start();
}
 
Example 12
Source Project: AndriodVideoCache   Source File: ProxyCacheTestUtils.java    License: Apache License 2.0 5 votes vote down vote up
public static int getPort(HttpProxyCacheServer server) {
    String proxyUrl = server.getProxyUrl("test");
    Pattern pattern = Pattern.compile("http://127.0.0.1:(\\d*)/test");
    Matcher matcher = pattern.matcher(proxyUrl);
    assertThat(matcher.find()).isTrue();
    String portAsString = matcher.group(1);
    return Integer.parseInt(portAsString);
}
 
Example 13
Source Project: DKVideoPlayer   Source File: ADActivity.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected void initView() {
    super.initView();
    mVideoView = findViewById(R.id.video_view);
    mController = new StandardVideoController(this);
    AdControlView adControlView = new AdControlView(this);
    adControlView.setListener(new AdControlView.AdControlListener() {
        @Override
        public void onAdClick() {
            Toast.makeText(ADActivity.this, "广告点击跳转", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onSkipAd() {
            playVideo();
        }
    });
    mController.addControlComponent(adControlView);

    HttpProxyCacheServer cacheServer = ProxyVideoCacheManager.getProxy(this);
    String proxyUrl = cacheServer.getProxyUrl(URL_AD);
    mVideoView.setUrl(proxyUrl);
    mVideoView.setVideoController(mController);

    //监听播放结束
    mVideoView.addOnStateChangeListener(new VideoView.SimpleOnStateChangeListener() {
        @Override
        public void onPlayStateChanged(int playState) {
            if (playState == VideoView.STATE_PLAYBACK_COMPLETED) {
                playVideo();
            }
        }
    });

    mVideoView.start();
}
 
Example 14
Source Project: DKVideoPlayer   Source File: ProxyVideoCacheManager.java    License: Apache License 2.0 5 votes vote down vote up
private static HttpProxyCacheServer newProxy(Context context) {
        return new HttpProxyCacheServer.Builder(context)
                .maxCacheSize(512 * 1024 * 1024)       // 512MB for cache
                //缓存路径,不设置默认在sd_card/Android/data/[app_package_name]/cache中
//                .cacheDirectory()
                .build();
    }
 
Example 15
Source Project: QSVideoPlayer   Source File: MainActivity.java    License: Apache License 2.0 5 votes vote down vote up
private void cacheConfig() {
    Proxy.setConfig(new HttpProxyCacheServer
                    .Builder(this)
                    .cacheDirectory(new File("/sdcard/video"))
                    //.fileNameGenerator() 存储文件名规则
                    .maxCacheSize(512 * 1024 * 1024)//缓存文件大小
            //.maxCacheFilesCount(100)//缓存文件数目 二选一

    );
}
 
Example 16
Source Project: QSVideoPlayer   Source File: Proxy.java    License: Apache License 2.0 5 votes vote down vote up
synchronized static HttpProxyCacheServer getProxy(Context context, final Map<String, String> headers) {
    if (builder == null)
        builder = new HttpProxyCacheServer.Builder(context);
    if (headers != null)
        builder.headerInjector(new HeaderInjector() {
            @Override
            public Map<String, String> addHeaders(String url) {
                return headers;
            }
        });
    return builder.build();
}
 
Example 17
Source Project: GSYVideoPlayer   Source File: ProxyCacheManager.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void doCacheLogic(Context context, IMediaPlayer mediaPlayer, String originUrl, Map<String, String> header, File cachePath) {
    String url = originUrl;
    userAgentHeadersInjector.mMapHeadData.clear();
    if (header != null) {
        userAgentHeadersInjector.mMapHeadData.putAll(header);
    }
    if (url.startsWith("http") && !url.contains("127.0.0.1") && !url.contains(".m3u8")) {
        HttpProxyCacheServer proxy = getProxy(context.getApplicationContext(), cachePath);
        if (proxy != null) {
            //此处转换了url,然后再赋值给mUrl。
            url = proxy.getProxyUrl(url);
            mCacheFile = (!url.startsWith("http"));
            //注册上缓冲监听
            if (!mCacheFile) {
                proxy.registerCacheListener(this, originUrl);
            }
        }
    } else if ((!url.startsWith("http") && !url.startsWith("rtmp")
            && !url.startsWith("rtsp") && !url.contains(".m3u8"))) {
        mCacheFile = true;
    }
    try {
        mediaPlayer.setDataSource(context, Uri.parse(url), header);
    } catch (IOException e) {
        e.printStackTrace();
    }

}
 
Example 18
Source Project: GSYVideoPlayer   Source File: ProxyCacheManager.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public boolean cachePreview(Context context, File cacheDir, String url) {
    HttpProxyCacheServer proxy = getProxy(context.getApplicationContext(), cacheDir);
    if (proxy != null) {
        //此处转换了url,然后再赋值给mUrl。
        url = proxy.getProxyUrl(url);
    }
    return (!url.startsWith("http"));
}
 
Example 19
Source Project: GSYVideoPlayer   Source File: ProxyCacheManager.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 创建缓存代理服务,带文件目录的.
 */
public HttpProxyCacheServer newProxy(Context context, File file) {
    if (!file.exists()) {
        file.mkdirs();
    }
    HttpProxyCacheServer.Builder builder = new HttpProxyCacheServer.Builder(context);
    builder.cacheDirectory(file);
    builder.maxCacheSize(DEFAULT_MAX_SIZE);
    builder.headerInjector(userAgentHeadersInjector);
    if (fileNameGenerator != null) {
        builder.fileNameGenerator(fileNameGenerator);
    }
    mCacheDir = file;
    return builder.build();
}
 
Example 20
Source Project: TigerVideo   Source File: Config.java    License: Apache License 2.0 5 votes vote down vote up
private HttpProxyCacheServer buildCacheProxy() {

            return new HttpProxyCacheServer
                    .Builder(context.getApplicationContext())
                    .cacheDirectory(new File(Utils.getCacheDir(context)))
                    .fileNameGenerator(new Md5FileNameGenerator() {
                        @Override
                        public String generate(String url) {

                            return ProxyCacheUtils.computeMD5(url);
                        }
                    })
                    .maxCacheFilesCount(20)
                    .build();
        }
 
Example 21
Source Project: AndroidVideoCache   Source File: VideoFragment.java    License: Apache License 2.0 5 votes vote down vote up
private void checkCachedState() {
    HttpProxyCacheServer proxy = App.getProxy(getActivity());
    boolean fullyCached = proxy.isCached(url);
    setCachedState(fullyCached);
    if (fullyCached) {
        progressBar.setSecondaryProgress(100);
    }
}
 
Example 22
Source Project: AndroidVideoCache   Source File: VideoFragment.java    License: Apache License 2.0 5 votes vote down vote up
private void startVideo() {
    HttpProxyCacheServer proxy = App.getProxy(getActivity());
    proxy.registerCacheListener(this, url);
    String proxyUrl = proxy.getProxyUrl(url);
    Log.d(LOG_TAG, "Use proxy url " + proxyUrl + " instead of original url " + url);
    videoView.setVideoPath(proxyUrl);
    videoView.start();
}
 
Example 23
Source Project: AndroidVideoCache   Source File: ProxyCacheTestUtils.java    License: Apache License 2.0 5 votes vote down vote up
public static int getPort(HttpProxyCacheServer server) {
    String proxyUrl = server.getProxyUrl("test");
    Pattern pattern = Pattern.compile("http://127.0.0.1:(\\d*)/test");
    Matcher matcher = pattern.matcher(proxyUrl);
    assertThat(matcher.find()).isTrue();
    String portAsString = matcher.group(1);
    return Integer.parseInt(portAsString);
}
 
Example 24
Source Project: youqu_master   Source File: VideoCacheManager.java    License: Apache License 2.0 4 votes vote down vote up
public static HttpProxyCacheServer getProxy(Context context) {
    return sharedProxy == null ? (sharedProxy = newProxy(context)) : sharedProxy;
}
 
Example 25
Source Project: youqu_master   Source File: VideoCacheManager.java    License: Apache License 2.0 4 votes vote down vote up
private static HttpProxyCacheServer newProxy(Context context) {
    return new HttpProxyCacheServer.Builder(context)
            .maxCacheSize(1024 * 1024 * 1024)       // 1 Gb for cache
            .build();
}
 
Example 26
Source Project: youqu_master   Source File: BaseIjkVideoView.java    License: Apache License 2.0 4 votes vote down vote up
private HttpProxyCacheServer getCacheServer() {
    return VideoCacheManager.getProxy(getContext().getApplicationContext());
}
 
Example 27
Source Project: imsdk-android   Source File: HttpProxyUtil.java    License: MIT License 4 votes vote down vote up
public static HttpProxyCacheServer getProxy(Context context) {
    return proxy == null ? (proxy = newProxy(context)) : proxy;
}
 
Example 28
Source Project: imsdk-android   Source File: ProxyFactory.java    License: MIT License 4 votes vote down vote up
public static HttpProxyCacheServer getProxy(Context context) {
    return sharedProxy == null ? (sharedProxy = newProxy(context)) : sharedProxy;
}
 
Example 29
Source Project: imsdk-android   Source File: ProxyFactory.java    License: MIT License 4 votes vote down vote up
private static HttpProxyCacheServer newProxy(Context context) {
    return new HttpProxyCacheServer(context);
}
 
Example 30
Source Project: AndriodVideoCache   Source File: GalleryVideoFragment.java    License: Apache License 2.0 4 votes vote down vote up
private void startProxy() {
    HttpProxyCacheServer proxy = App.getProxy(getActivity());
    proxy.registerCacheListener(this, url);
    videoView.setVideoPath(proxy.getProxyUrl(url));
}