视频缓存VideoCache

做Android需要做简单的视频缓存  videocache

首先为依赖包:

 compile 'com.danikula:videocache:2.7.1'

VideoCache工具类

package com.ys.hwpush.videoview;

import android.annotation.SuppressLint;
import android.content.Context;

import com.danikula.videocache.HttpProxyCacheServer;
import com.danikula.videocache.file.FileNameGenerator;
import java.io.File;

/**
 * videocache 本地缓存、播放代理类 Created by liangzhen on 2018/07/30.
 */
public class VideoCacheManager {

    private Context context;

    private HttpProxyCacheServer httpProxyCacheServer;

    @SuppressLint("StaticFieldLeak")
    private static volatile VideoCacheManager videoCacheManager;

    private String feed;

    private VideoCacheManager(Context context){
        this.context = context;
    }

    public static VideoCacheManager getVideoCacheManager(Context context) {
        if (videoCacheManager == null){
            synchronized (VideoCacheManager.class){
                if (videoCacheManager == null){
                    videoCacheManager = new VideoCacheManager(context);
                }
            }
        }
        return videoCacheManager;
    }

    //设置一下ID(用于缓存的名字)
    public VideoCacheManager setFeed(String feed){
        this.feed = feed;
        return this;
    }

    public String getVideoCacheUriDir(){
        String videoCachePath = context.getExternalFilesDir("video_cache").getAbsolutePath();
        makeMultiDirs(videoCachePath);
        return videoCachePath;
    }

    public HttpProxyCacheServer getProxy() {
        if(httpProxyCacheServer == null){
            httpProxyCacheServer = newProxy();
        }
        return httpProxyCacheServer;
    }

    //用于做一些缓存规则
    private HttpProxyCacheServer newProxy() {
        return new HttpProxyCacheServer.Builder(context)
                .cacheDirectory(new File(getVideoCacheUriDir()))
                .maxCacheFilesCount(5)
                .maxCacheSize(1024 * 1024 * 300)
                .fileNameGenerator(new FileNameGenerator() {
                    @Override
                    public String generate(String url) {
                        return feed == null ? "" : feed + ".mp4";
                    }
                })
                .build();
    }


    //创建文件夹
    public static boolean makeMultiDirs(String dirs) {
        boolean mkOK = false;
        File file = new File(dirs);
        if (!file.exists()) {
            mkOK = file.mkdirs();
        }
        return mkOK;
    }

}

Activity的代码模拟

package com.ys.hwpush.videoview;

import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;

import com.danikula.videocache.HttpProxyCacheServer;
import com.ys.hwpush.R;

import java.util.ArrayList;
import java.util.List;

public class VideoPlayActivity extends AppCompatActivity {

    private VideoCacheManager cacheManager;

    //A是作为下标操作
    private int A=0;

    private List<String> list;
    private VideoView videoView;
    private MediaController controller;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_play);

        list = new ArrayList<>();
        list.add("http://cdn1.cooolar.com/feed_video/2018-04-26/15247317868073.mp4");
        list.add("http://cdn1.cooolar.com/feed_video/2018-02-25/15195224284882.mp4");
        list.add("http://cdn1.cooolar.com/feed_video/2018-04-05/15229203431059.mp4");
        list.add("http://cdn1.cooolar.com/feed_video/2018-03-16/15212035563102.mp4");
        list.add("http://cdn1.cooolar.com/feed_video/2018-03-23/15217943047868.mp4");
        list.add("http://cdn1.cooolar.com/feed_video/2018-04-12/15235107918092.mp4");
        list.add("http://cdn1.cooolar.com/feed_video/2018-03-24/15218702781120.mp4");
        list.add("http://cdn1.cooolar.com/feed_video/2018-03-30/15224013517025.mp4");


        Button up = findViewById(R.id.but_up);
        Button but= findViewById(R.id.but);
        videoView = findViewById(R.id.videoView);

        cacheManager = VideoCacheManager.getVideoCacheManager(this);

        controller = new MediaController(this);

        playVideo(A);

        up.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                A=A-1;
                if(A<0){
                    A=A+1;
                    Toast.makeText(VideoPlayActivity.this,"上面没有视频了",Toast.LENGTH_SHORT).show();
                }else{
                    playVideo(A);
                }
            }
        });

        but.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                A=A+1;
                if(A>list.size()-1){
                    A=A-1;
                    Toast.makeText(VideoPlayActivity.this,"没有更多视频了",Toast.LENGTH_SHORT).show();
                }else{
                    playVideo(A);
                }
            }
        });

    }

    public void playVideo(int A){

        videoView.stopPlayback();

        cacheManager.setFeed("video"+A);
        HttpProxyCacheServer proxy = cacheManager.getProxy();
        String proxyUrl = proxy.getProxyUrl(list.get(A));

        Uri uri = Uri.parse(proxyUrl);
        //设置视频路径
        videoView.setVideoURI(uri);

        //关联管理器
        videoView.setMediaController(controller);
        controller.setAnchorView(videoView);
        //开始播放视频
        videoView.start();
    }


    //拦截了返回键
    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        return false;
    }
}

布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".videoview.VideoPlayActivity">

    <VideoView
        android:id="@+id/videoView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <Button
        android:id="@+id/but_up"
        android:text="上一個"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />


    <Button
        android:layout_below="@id/but_up"
        android:id="@+id/but"
        android:text="下一個"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值