码迷,mamicode.com
首页 > 移动开发 > 详细

android -------- VideoCache 视频播放(缓存视频到本地)

时间:2019-10-24 21:13:40      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:视频播放   focus   cache   layout   csdn   androi   instance   做了   ring   

 

先前做了一个小视频的功能,里面有播放多个视频的功能,为了效率,我加了视频缓存功能;

一方面耗费用户的流量,另一方面直接从本地播放要更流畅

网上看资料,一个视频缓存库,使用起来很方便,还不错,就分享给大家

   //视频缓存
    implementation ‘com.danikula:videocache:2.7.1‘

 

效果

          技术图片

 

代码:

 

public class MainActivity extends AppCompatActivity {

    private static final int MY_PERMISSIONS_WRITE_EXTERNAL_STORAGE = 1;

    String url = "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4";
    VideoView videoView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        videoView = findViewById(R.id.videoView);

        //检查版本是否大于M
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(this,
                        new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                        MY_PERMISSIONS_WRITE_EXTERNAL_STORAGE);
            } else {
                Log.i("aaa", "权限已申请");
                initVideo();
            }
        }
    }

    private void initVideo() {
        HttpProxyCacheServer proxy = VideoApplication.getProxy(this);
        //1.我们会将原始url注册进去
        // proxy.registerCacheListener(, bean.getVideo_url());
        //2.我们播放视频的时候会调用以下代码生成proxyUrl
        String proxyUrl = proxy.getProxyUrl(url);
        if (proxy.isCached(url)) {
            Log.i("aaaa", "已缓存");
        } else {
            Log.i("aaaa", "未缓存");
        }
        Log.i("aaaapath", proxyUrl);
        videoView.setVideoPath(proxyUrl);
        videoView.start();
        videoView.findFocus();
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {

        if (requestCode == MY_PERMISSIONS_WRITE_EXTERNAL_STORAGE) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                initVideo();
            } else {
                //"权限已拒绝";
            }
        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

}

 

日志:

技术图片

 

这样可以在本地的磁盘里找到视频了

 

Application代码:

public class VideoApplication extends Application {


    @Override
    public void onCreate() {
        super.onCreate();
    }

    private HttpProxyCacheServer proxy;

    public static HttpProxyCacheServer getProxy(Context context) {
        VideoApplication app = (VideoApplication) context.getApplicationContext();
        return app.proxy == null ? (app.proxy = app.newProxy()) : app.proxy;
    }

    private HttpProxyCacheServer newProxy() {
        return new HttpProxyCacheServer.Builder(this)
                .maxCacheSize(1024 * 1024 * 1024)       // 1 Gb for cache
                .fileNameGenerator(new MyFileNameGenerator())
                .build();
    }
}

 

记住不要忘记了AndroidManifest权限

  <uses-permission android:name="android.permission.INTERNET" />
    <!--用于写入缓存数据到扩展存储卡-->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.VIBRATE" />

 

代码下载:

  https://github.com/DickyQie/android-video/tree/video-cache

参考文档(库地址)

https://github.com/danikula/AndroidVideoCache

https://blog.csdn.net/zhqw_csdn/article/details/81514313

 

android -------- VideoCache 视频播放(缓存视频到本地)

标签:视频播放   focus   cache   layout   csdn   androi   instance   做了   ring   

原文地址:https://www.cnblogs.com/zhangqie/p/11730788.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!