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


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

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

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

       //视频缓存
        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

  • 相关阅读:
    关于int8_t,uint8_t.....等数据类型的理解
    导出函数__declspec(dllexport)
    c++中的namespace
    GitHub C 和 C++ 开源库的清单(含示例代码)
    进制之间的转换一
    为 JSON 字符串创建对象
    HTML5 简单Demo1
    主要浏览器的调试工具
    fiddler2抓包工具使用图文教程
    JAVA将秒的总和转换成时分秒的格式
  • 原文地址:https://www.cnblogs.com/zhangqie/p/11730788.html
Copyright © 2020-2023  润新知