CacheVideoByUrlActivity.java 2.44 KB
package com.cnlive.demo.video.cache;

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

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

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

public class CacheVideoByUrlActivity extends VideoActivity {

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

    private String url;

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

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

        proxyService = CacheManager.getProxyService(this);
        int cachePercent = proxyService.getCachingPercent(url);
        setVideoSecondaryProgress(cachePercent);

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

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

        startPlayByUrl(playerUrl);
    }

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

    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 (CacheVideoByUrlActivity.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);
        }
    };
}