ソースを参照

添加视频缓存库,提升下次播放速度

zengjiebin 7 年 前
コミット
200cc4f896

+ 2 - 0
app/build.gradle

@@ -291,6 +291,8 @@ dependencies {
     //有米科技
     implementation(name: 'YoumiSdk_v8.3.0_2018-09-20', ext: 'aar')
     implementation project(':WaterWaveProgress')
+    //AndroidVideoCache 网络视频缓存 https://github.com/danikula/AndroidVideoCache
+    implementation 'com.danikula:videocache:2.7.1'
 }
 
 static def releaseTime() {

+ 18 - 0
app/src/main/java/com/sheep/gamegroup/util/FileUtil.java

@@ -193,6 +193,24 @@ public class FileUtil {
         return filename;
     }
 
+    /**
+     * 获取文件名,带扩展名
+     * @param filename 前提是fileName前面一定没有/
+     * @param defaultEx 默认的扩展
+     * @return
+     */
+    public static String getFileName(String filename, String defaultEx) {
+        if ((filename != null) && (filename.length() > 0)) {
+            int dot = filename.lastIndexOf('.');
+            if ((dot > -1) && (dot < (filename.length()))) {
+                return filename;
+            } else {
+                return filename + "." + defaultEx;
+            }
+        }
+        return filename;
+    }
+
 
 
 

+ 7 - 1
app/src/main/java/com/sheep/gamegroup/util/Jump2View.java

@@ -12,6 +12,7 @@ import android.text.TextUtils;
 import android.view.Gravity;
 import android.view.View;
 
+import com.danikula.videocache.HttpProxyCacheServer;
 import com.sheep.gamegroup.absBase.BaseActivity;
 import com.sheep.gamegroup.absBase.IHomePageSearch;
 import com.sheep.gamegroup.greendao.download.DownLoadInfo;
@@ -2200,9 +2201,14 @@ public class Jump2View {
      * 播放视频
      *
      * @param url 播放地址
+     * @param isCache 是否缓存,方便以后播放
      */
-    public void goActPlayVideo(String url) {
+    public void goActPlayVideo(String url, boolean isCache) {
         Activity activity = ActivityManager.getInstance().currentActivity();
+        if(isCache) {
+            HttpProxyCacheServer proxy = SheepApp.getHttpProxyCacheServer();
+            url = proxy.getProxyUrl(url);
+        }
         activity.startActivity(DataUtil.putObject(new Intent(activity, ActPlayVideo.class), url));
     }
 

+ 18 - 0
app/src/main/java/com/sheep/gamegroup/util/MyFileNameGenerator.java

@@ -0,0 +1,18 @@
+package com.sheep.gamegroup.util;
+
+import android.net.Uri;
+
+import com.danikula.videocache.file.FileNameGenerator;
+
+/**
+ * Created by realicing on 2018/11/15.
+ * realicing@sina.com
+ */
+public class MyFileNameGenerator implements FileNameGenerator {
+    @Override
+    public String generate(String url) {
+        Uri uri = Uri.parse(url);
+        String lLastPathSegment = uri.getLastPathSegment();
+        return FileUtil.getFileName(lLastPathSegment, "mp4");
+    }
+}

+ 1 - 1
app/src/main/java/com/sheep/gamegroup/view/adapter/AdpNoviceGuidance.java

@@ -54,7 +54,7 @@ public class AdpNoviceGuidance extends RecyclerViewAdapter<NoviceGuidance> {
         item_novice_guidance_video_control.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
-                Jump2View.getInstance().goActPlayVideo(item.getVideo_url());
+                Jump2View.getInstance().goActPlayVideo(item.getVideo_url(), true);
             }
         });
 //        if(!viewHolderList.contains(viewHolder) && !TextUtils.isEmpty(item.getVideo_url()))

+ 18 - 0
app/src/main/java/com/sheep/jiuyan/samllsheep/SheepApp.java

@@ -16,6 +16,7 @@ import com.baidu.location.BDLocation;
 import com.baidu.location.LocationClient;
 import com.baidu.location.LocationClientOption;
 import com.bumptech.glide.Glide;
+import com.danikula.videocache.HttpProxyCacheServer;
 import com.liulishuo.okdownload.OkDownload;
 import com.liulishuo.okdownload.core.dispatcher.DownloadDispatcher;
 import com.sheep.gamegroup.di.components.DaggerNetComponent;
@@ -32,6 +33,7 @@ import com.sheep.gamegroup.util.DataKey;
 import com.sheep.gamegroup.util.DataUtil;
 import com.sheep.gamegroup.util.Jump2View;
 import com.sheep.gamegroup.util.LogUtil;
+import com.sheep.gamegroup.util.MyFileNameGenerator;
 import com.sheep.gamegroup.util.RefreshUtil;
 import com.sheep.gamegroup.util.SysAppUtil;
 import com.sheep.gamegroup.util.UMConfigUtils;
@@ -354,6 +356,22 @@ public class SheepApp extends MultiDexApplication {
     }
 
 
+    private HttpProxyCacheServer proxy;
+
+    //AndroidVideoCache 获取缓存代表服务器,可缓存视频
+    //https://github.com/danikula/AndroidVideoCache
+    public static HttpProxyCacheServer getHttpProxyCacheServer() {
+        return mSheepApp.proxy == null ? (mSheepApp.proxy = mSheepApp.newHttpProxyCacheServer()) : mSheepApp.proxy;
+    }
+
+    private HttpProxyCacheServer newHttpProxyCacheServer() {
+        return new HttpProxyCacheServer.Builder(this)
+                .maxCacheSize(2L * 1024 * 1024 * 1024)       // 2 Gb for cache
+                .maxCacheFilesCount(20)////最多缓存个数
+                .fileNameGenerator(new MyFileNameGenerator())
+                .build();
+    }
+
     public int mActivityCount;//显示的activity的个数, 0个时代表暂停或退出应用,1个代表应用正显示在用户面前
     private boolean notStop = false;
     /**