CacheVideoActivity.java 3.34 KB
package com.cnlive.demo.video.cache;

import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.util.Log;

import com.cnlive.libs.util.SharedPreferencesHelper;
import com.cnlive.libs.video.cache.CacheManager;
import com.cnlive.libs.video.cache.ICache;
import com.cnlive.libs.video.cache.ProxyService;

import java.io.File;

/**
 * Created by gujun on 2017/10/20.
 */

public class CacheVideoActivity extends VideoActivity {

    private static final String TAG = "CacheVideoActivity";
    private ProxyService proxyService;

    private String url;

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

    private void initCache() {
        url = getIntent().getStringExtra("url");

        proxyService = CacheManager.getProxyService(this);

        SharedPreferencesHelper sph = SharedPreferencesHelper.getInstance(this);
        if (sph.getBoolean(CacheActivity.CACHE_LIMIT_SIZE, false)) {
            proxyService.setMaxCacheSize(1000 * 1024 * 1024);
        } else {
            proxyService.setMaxFilesCount(500);
        }

        proxyService.setCacheRoot(setCacheFilePath());
        proxyService.startServer();

        int cachePercent = proxyService.getCachingPercent(url);
        setVideoSecondaryProgress(cachePercent);

        //开启缓存后才能使用此地址播放
        playerUrl = proxyService.getProxyUrl(url);

        proxyService.registerCacheStatusListener(mCacheStatusListener, url);
        proxyService.registerErrorListener(mOnErrorListener);
        proxyService.registerLogEventListener(mOnLogEventListener);

        startPlay();
    }

    /**
     * 设置播放缓存路径
     *
     * @return
     */
    private File setCacheFilePath() {
        String url = Environment.getExternalStorageDirectory().toString() + File.separator + getPackageName() + File.separator + "cache";
        File file = new File(url);
        if (!file.exists())
            file.mkdirs();
        return file;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(proxyService != null){
            proxyService.unregisterCacheStatusListener(mCacheStatusListener, url);
            proxyService.unregisterErrorListener(mOnErrorListener);
            proxyService.unregisterLogEventListener(mOnLogEventListener);
            proxyService.shutDownServer();
        }
    }

    private ICache.OnCacheStatusListener mCacheStatusListener = new ICache.OnCacheStatusListener() {
        @Override
        public void OnCacheStatus(String url, long sourceLength, int percentsAvailable) {
            Log.e(TAG, "OnCacheStatus: url_" + url + "\nsourceLength:" + sourceLength + "\npercentsAvailable" + percentsAvailable);
            if (CacheVideoActivity.this.url.equals(url)) {
                setVideoSecondaryProgress(percentsAvailable);
            }
        }
    };

    private ICache.OnErrorListener mOnErrorListener = new ICache.OnErrorListener() {
        @Override
        public void OnError(int errCode) {
            Log.e(TAG, "OnError: " + errCode);
        }
    };

    private ICache.OnLogEventListener mOnLogEventListener = new ICache.OnLogEventListener() {
        @Override
        public void onLogEvent(String log) {
            Log.e(TAG, "onLogEvent: " + log);
        }
    };
}