Преглед на файлове

优化所有视频播放页面播放速度

zengjiebin преди 7 години
родител
ревизия
380c07c9f5

+ 17 - 3
app/src/main/java/com/sheep/gamegroup/module/game/activity/ActGameGroupOrGameDetail.java

@@ -22,6 +22,7 @@ import com.scwang.smartrefresh.layout.SmartRefreshLayout;
 import com.scwang.smartrefresh.layout.api.RefreshLayout;
 import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
 import com.sheep.gamegroup.absBase.AbsGetDownloadListener;
+import com.sheep.gamegroup.absBase.AbsObserver;
 import com.sheep.gamegroup.absBase.BaseActivity;
 import com.sheep.gamegroup.absBase.ILoadMore;
 import com.sheep.gamegroup.absBase.IRefresh;
@@ -618,7 +619,6 @@ public class ActGameGroupOrGameDetail extends BaseActivity {
                             if (videoPlayer != null)
                                 videoPlayer.startPlayLogic();
                         }
-
                         @Override
                         public void onPlayError(String url, Object... objects) {
                             super.onPlayError(url, objects);
@@ -628,9 +628,23 @@ public class ActGameGroupOrGameDetail extends BaseActivity {
                             if(file.exists()){
                                 LogUtil.println("initPlay", "retry play");
                                 if (videoPlayer != null) {
-                                    if(curPosition > 0)
+                                    if(curPosition > 0) {
                                         videoPlayer.setSeekOnStart(curPosition);
-                                    videoPlayer.startPlayLogic();
+                                        videoPlayer.startPlayLogic();
+                                    } else {
+                                        ViewUtil.delay(new AbsObserver<Integer>(){
+                                            @Override
+                                            public void onNext(Integer integer) {
+                                                super.onNext(integer);
+                                                try {
+                                                    if(videoPlayer != null)
+                                                        videoPlayer.startPlayLogic();
+                                                } catch (Exception e) {
+                                                    e.printStackTrace();
+                                                }
+                                            }
+                                        }, 1);
+                                    }
                                 }
                             }
                         }

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

@@ -2492,9 +2492,16 @@ public class Jump2View {
      * @param coverUrl 封面
      */
     public void goActPlayVideo(String url, String title, String coverUrl) {
+        String fileName = FileUtil.getLastSplitEnd(url, '/');
+        if(!fileName.endsWith(".mp4")){
+            fileName +=  ".mp4";
+        }
+        String filePath = new File(DIR, fileName).getAbsolutePath();
+        //开始下载视频
+        Jump2View.getInstance().startDownloadService(url, filePath);
         Activity activity = SheepApp.getInstance().getCurrentActivity();
         Intent intent = new Intent(activity, ActPlayVideo.class);
-        intent.putExtra(ActPlayVideo.KEY_URL, url);
+        intent.putExtra(ActPlayVideo.KEY_URL, filePath);
         if(TextUtils.isEmpty(title)){//标题最不可靠,可能为空
             title = url;
         }

+ 31 - 1
app/src/main/java/com/sheep/gamegroup/view/activity/ActPlayVideo.java

@@ -21,6 +21,7 @@ import com.shuyu.gsyvideoplayer.builder.GSYVideoOptionBuilder;
 import com.shuyu.gsyvideoplayer.listener.GSYSampleCallBack;
 import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer;
 
+import java.io.File;
 import java.util.Locale;
 
 import butterknife.BindView;
@@ -99,7 +100,7 @@ public class ActPlayVideo extends BaseActivity {
                     .setShowFullAnimation(false)
                     .setNeedLockFull(true)
                     .setUrl(getVideoUrl())
-                    .setCacheWithPlay(true)
+                    .setCacheWithPlay(false)
                     .setVideoTitle(getVideoTitle())
                     .setVideoAllCallBack(new GSYSampleCallBack() {
                         @Override
@@ -114,6 +115,35 @@ public class ActPlayVideo extends BaseActivity {
                             super.onAutoComplete(url, objects);
                             videoPlayer.startPlayLogic();
                         }
+                        @Override
+                        public void onPlayError(String url, Object... objects) {
+                            super.onPlayError(url, objects);
+                            File file = new File(video_url);
+                            long curPosition = GSYVideoManager.instance().getCurPlayerManager().getCurrentPosition();
+                            LogUtil.println("initPlay", "onPlayError", url, curPosition);
+                            if(file.exists()){
+                                LogUtil.println("initPlay", "retry play");
+                                if (videoPlayer != null) {
+                                    if(curPosition > 0) {
+                                        videoPlayer.setSeekOnStart(curPosition);
+                                        videoPlayer.startPlayLogic();
+                                    } else {
+                                        ViewUtil.delay(new AbsObserver<Integer>(){
+                                            @Override
+                                            public void onNext(Integer integer) {
+                                                super.onNext(integer);
+                                                try {
+                                                    if(videoPlayer != null)
+                                                        videoPlayer.startPlayLogic();
+                                                } catch (Exception e) {
+                                                    e.printStackTrace();
+                                                }
+                                            }
+                                        }, 1);
+                                    }
+                                }
+                            }
+                        }
                     }).build(videoPlayer);
             isInit = true;
         } else {

+ 40 - 2
app/src/main/java/com/sheep/gamegroup/view/activity/ActVideoDetail.java

@@ -22,6 +22,7 @@ import com.sheep.gamegroup.model.util.SheepSubscriber;
 import com.sheep.gamegroup.util.ApiJSONUtil;
 import com.sheep.gamegroup.util.ApiUtil;
 import com.sheep.gamegroup.util.DataUtil;
+import com.sheep.gamegroup.util.FileUtil;
 import com.sheep.gamegroup.util.Jump2View;
 import com.sheep.gamegroup.util.ListUtil;
 import com.sheep.gamegroup.util.LogUtil;
@@ -38,6 +39,7 @@ import com.shuyu.gsyvideoplayer.listener.GSYSampleCallBack;
 import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer;
 import com.umeng.socialize.bean.SHARE_MEDIA;
 
+import java.io.File;
 import java.util.ArrayList;
 
 import butterknife.BindView;
@@ -45,6 +47,8 @@ import io.reactivex.android.schedulers.AndroidSchedulers;
 import io.reactivex.disposables.Disposable;
 import io.reactivex.schedulers.Schedulers;
 
+import static com.sheep.jiuyan.samllsheep.utils.ClassFileHelper.DIR;
+
 /**
  * Created by realicing on 2018/11/9.
  * realicing@sina.com
@@ -115,9 +119,14 @@ public class ActVideoDetail extends BaseUMActivity {
     }
 
     private void initVideoData(DiscoveryVideo lastVideo) {
+        String url = lastVideo.getResource();
+        String fileName = FileUtil.getLastSplitEnd(url, '/') + ".mp4";
+        String filePath = new File(DIR, fileName).getAbsolutePath();
+        //开始下载视频
+        Jump2View.getInstance().startDownloadService(url, filePath);
         video_id = lastVideo.getId();
         user_id = lastVideo.getUser_id();
-        video_url = lastVideo.getResource();
+        video_url = filePath;
         video_title = lastVideo.getTitle();
         video_cover_url = lastVideo.getCover();
         loadVideoData(lastVideo);
@@ -157,7 +166,7 @@ public class ActVideoDetail extends BaseUMActivity {
                     .setShowFullAnimation(false)
                     .setNeedLockFull(true)
                     .setUrl(getVideoUrl())
-                    .setCacheWithPlay(true)
+                    .setCacheWithPlay(false)
                     .setVideoTitle(getVideoTitle())
                     .setVideoAllCallBack(new GSYSampleCallBack() {
                         @Override
@@ -172,6 +181,35 @@ public class ActVideoDetail extends BaseUMActivity {
                             super.onAutoComplete(url, objects);
                             videoPlayer.startPlayLogic();
                         }
+                        @Override
+                        public void onPlayError(String url, Object... objects) {
+                            super.onPlayError(url, objects);
+                            File file = new File(video_url);
+                            long curPosition = GSYVideoManager.instance().getCurPlayerManager().getCurrentPosition();
+                            LogUtil.println("initPlay", "onPlayError", url, curPosition);
+                            if(file.exists()){
+                                LogUtil.println("initPlay", "retry play");
+                                if (videoPlayer != null) {
+                                    if(curPosition > 0) {
+                                        videoPlayer.setSeekOnStart(curPosition);
+                                        videoPlayer.startPlayLogic();
+                                    } else {
+                                        ViewUtil.delay(new AbsObserver<Integer>(){
+                                            @Override
+                                            public void onNext(Integer integer) {
+                                                super.onNext(integer);
+                                                try {
+                                                    if(videoPlayer != null)
+                                                        videoPlayer.startPlayLogic();
+                                                } catch (Exception e) {
+                                                    e.printStackTrace();
+                                                }
+                                            }
+                                        }, 1);
+                                    }
+                                }
+                            }
+                        }
                     }).build(videoPlayer);
             isInit = true;
         } else {