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

使用GSYVideoPlayer来播放视频

zengjiebin преди 7 години
родител
ревизия
96e1bec08a

+ 5 - 1
app/build.gradle

@@ -407,7 +407,7 @@ dependencies {
     implementation project(':WaterWaveProgress')
 
     //AndroidVideoCache 网络视频缓存 https://github.com/danikula/AndroidVideoCache
-    implementation 'com.danikula:videocache:2.7.1'
+//    implementation 'com.danikula:videocache:2.7.1'
 
     //视频图片选择
 //    implementation ('cn.finalteam.rxgalleryfinal:library:1.1.3') {
@@ -432,6 +432,10 @@ dependencies {
 //    implementation 'com.github.yangjie10930:EpMedia:v0.9.5'
     //webView 缓存优化初始化 https://github.com/yale8848/CacheWebView
     implementation 'ren.yale.android:cachewebviewlib:2.1.4'
+    //GSYVideoPlayer https://github.com/CarGuo/GSYVideoPlayer
+    implementation 'com.shuyu:gsyVideoPlayer-java:6.0.2'
+//是否需要ExoPlayer模式
+    implementation 'com.shuyu:GSYVideoPlayer-exo2:6.0.2'
 }
 
 static def releaseTime() {

+ 19 - 1
app/proguard-rules.pro

@@ -488,4 +488,22 @@
 
 #okio
 -dontwarn okio.**
--keep class okio.**{*;}
+-keep class okio.**{*;}
+
+#==================GSYVideoPlayer https://github.com/CarGuo/GSYVideoPlayer==========================
+-keep class com.shuyu.gsyvideoplayer.video.** { *; }
+-dontwarn com.shuyu.gsyvideoplayer.video.**
+-keep class com.shuyu.gsyvideoplayer.video.base.** { *; }
+-dontwarn com.shuyu.gsyvideoplayer.video.base.**
+-keep class com.shuyu.gsyvideoplayer.utils.** { *; }
+-dontwarn com.shuyu.gsyvideoplayer.utils.**
+-keep class tv.danmaku.ijk.** { *; }
+-dontwarn tv.danmaku.ijk.**
+
+-keep public class * extends android.view.View{
+    *** get*();
+    void set*(***);
+    public <init>(android.content.Context);
+    public <init>(android.content.Context, android.util.AttributeSet);
+    public <init>(android.content.Context, android.util.AttributeSet, int);
+}

+ 93 - 159
app/src/main/java/com/sheep/gamegroup/view/activity/ActVideoDetail.java

@@ -2,8 +2,6 @@ package com.sheep.gamegroup.view.activity;
 
 import android.annotation.SuppressLint;
 import android.content.Intent;
-import android.media.MediaPlayer;
-import android.net.Uri;
 import android.os.Bundle;
 import android.support.annotation.Nullable;
 import android.support.design.widget.CheckableImageButton;
@@ -14,9 +12,7 @@ import android.view.MotionEvent;
 import android.view.View;
 import android.widget.ImageView;
 import android.widget.TextView;
-import android.widget.VideoView;
 
-import com.danikula.videocache.CacheListener;
 import com.kfzs.duanduan.utils.StatusBarUtils;
 import com.sheep.gamegroup.absBase.BaseUMActivity;
 import com.sheep.gamegroup.model.entity.BaseMessage;
@@ -31,12 +27,16 @@ import com.sheep.gamegroup.util.LogUtil;
 import com.sheep.gamegroup.util.TestUtil;
 import com.sheep.gamegroup.util.ViewUtil;
 import com.sheep.gamegroup.util.share.ShareLinkConfig;
+import com.sheep.gamegroup.view.customview.SheepGSYVideoPlayer;
 import com.sheep.jiuyan.samllsheep.R;
 import com.sheep.jiuyan.samllsheep.SheepApp;
 import com.sheep.jiuyan.samllsheep.utils.G;
+import com.shuyu.gsyvideoplayer.GSYVideoManager;
+import com.shuyu.gsyvideoplayer.builder.GSYVideoOptionBuilder;
+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;
@@ -48,7 +48,7 @@ import io.reactivex.schedulers.Schedulers;
  * realicing@sina.com
  * 小绵羊3.4.5新增 -- 视频详情
  */
-public class ActVideoDetail extends BaseUMActivity implements MediaPlayer.OnInfoListener, MediaPlayer.OnPreparedListener, MediaPlayer.OnErrorListener, MediaPlayer.OnCompletionListener, CacheListener {
+public class ActVideoDetail extends BaseUMActivity {
 
     @Override
     protected void onCreate(@Nullable Bundle savedInstanceState) {
@@ -57,10 +57,8 @@ public class ActVideoDetail extends BaseUMActivity implements MediaPlayer.OnInfo
     }
 
     //播放
-    @BindView(R.id.videoView)
-    VideoView videoView;
-    @BindView(R.id.videoCoverIv)
-    ImageView videoCoverIv;
+    @BindView(R.id.detail_player)
+    StandardGSYVideoPlayer videoPlayer;
     @BindView(R.id.video_loading)
     View video_loading;
 
@@ -92,7 +90,8 @@ public class ActVideoDetail extends BaseUMActivity implements MediaPlayer.OnInfo
     private int video_id;
     private int user_id;
     private String video_url;
-    private int orientation = 0;
+    private String video_cover_url;
+    private String video_title;
     private DiscoveryVideo newVideo;
     private ArrayList<DiscoveryVideo> videoList;
     private int position;
@@ -115,7 +114,8 @@ public class ActVideoDetail extends BaseUMActivity implements MediaPlayer.OnInfo
         video_id = lastVideo.getId();
         user_id = lastVideo.getUser_id();
         video_url = lastVideo.getResource();
-        orientation = lastVideo.getOrientation();
+        video_title = lastVideo.getTitle();
+        video_cover_url = lastVideo.getCover();
         loadVideoData(lastVideo);
     }
 
@@ -127,8 +127,6 @@ public class ActVideoDetail extends BaseUMActivity implements MediaPlayer.OnInfo
             ViewUtil.setVisibility2(video_focus_iv, false);
             return;
         }
-        ViewUtil.setVisibility(videoCoverIv, true);
-        ViewUtil.setImage(videoCoverIv, discoveryVideo.getCover());
         ViewUtil.setAvatar(video_avatar_iv, discoveryVideo.getAvatar());
         ViewUtil.setText(video_like_tv, discoveryVideo.getLike());
         ViewUtil.setText(video_title_tv, discoveryVideo.getTitle());
@@ -138,35 +136,47 @@ public class ActVideoDetail extends BaseUMActivity implements MediaPlayer.OnInfo
         ViewUtil.setChecked(video_focus_iv, discoveryVideo.isIs_focus_user());
     }
 
+    private boolean isPlay = false;
+    private boolean isPause = false;
+    private ImageView coverImageView;
+    private boolean isInit = false;
     private void initPlay() {
-        videoView.setOnInfoListener(this);
-        videoView.setOnPreparedListener(this);
-        videoView.setOnErrorListener(this);
-        videoView.setOnCompletionListener(this);
-//        videoView.setMediaController(new MediaController(this));
-        videoView.setVideoURI(getUri());
-//        videoView.setVideoPath(getUrl());
-//        videoView.start();
-        resetVideoOrientation();
-    }
-    //重新设置视频的方向,videoView没法设置方向
-    private void resetVideoOrientation(){
-//        if(orientation > 0 && orientation < 360)
-//            videoView.setRotation(orientation);
-    }
-    private boolean isResister = false;
-    //转化地址
-    private String getUrl(){
-        if(isResister)
-            SheepApp.getHttpProxyCacheServer().registerCacheListener(this, video_url);
-        return SheepApp.getHttpProxyCacheServer().getProxyUrl(video_url);
-    }
-    //转化地址
-    private Uri getUri(){
-        String proxyUrl = getUrl();
-        Uri uri = Uri.parse(proxyUrl);
-        LogUtil.println(ActVideoDetail.class.getSimpleName(),"\n", video_url, "\n", proxyUrl);
-        return uri;
+        if(!isInit) {
+            GSYVideoOptionBuilder gsyVideoOption = new GSYVideoOptionBuilder();
+            //增加封面
+            coverImageView = new ImageView(this);
+            coverImageView.setScaleType(ImageView.ScaleType.FIT_XY);
+            gsyVideoOption.setThumbImageView(coverImageView)
+                    .setIsTouchWiget(false)
+                    .setRotateViewAuto(true)
+                    .setLockLand(true)
+                    .setAutoFullWithSize(true)
+                    .setShowFullAnimation(false)
+                    .setNeedLockFull(true)
+                    .setUrl(getVideoUrl())
+                    .setCacheWithPlay(true)
+                    .setVideoTitle(getVideoTitle())
+                    .setVideoAllCallBack(new GSYSampleCallBack() {
+                        @Override
+                        public void onPrepared(String url, Object... objects) {
+                            super.onPrepared(url, objects);
+                            ViewUtil.setVisibility(video_loading, false);
+                            isPlay = true;
+                        }
+
+                        @Override
+                        public void onAutoComplete(String url, Object... objects) {
+                            super.onAutoComplete(url, objects);
+                            videoPlayer.startPlayLogic();
+                        }
+                    }).build(videoPlayer);
+            isInit = true;
+        } else {
+            videoPlayer.setUp(getVideoUrl(), true, getVideoTitle());
+        }
+        videoPlayer.startPlayLogic();
+        ViewUtil.setVisibility(videoPlayer.getThumbImageViewLayout(), true);
+        ViewUtil.setImage(coverImageView, getVideoCoverUrl());
     }
 
     //点赞
@@ -278,13 +288,8 @@ public class ActVideoDetail extends BaseUMActivity implements MediaPlayer.OnInfo
     public void initListener() {
         initPlay();
         mGestureDetector = new GestureDetector(getApplicationContext(), mGestureListener);
-        View.OnTouchListener listener = new View.OnTouchListener() {
-            @Override
-            public boolean onTouch(View v, MotionEvent event) {
-                return onTouchEvent(event);
-            }
-        };
-        videoView.setOnTouchListener(listener);
+        if(videoPlayer instanceof SheepGSYVideoPlayer)
+            ((SheepGSYVideoPlayer) videoPlayer).setGestureDetector(mGestureDetector);
     }
 
     @Override
@@ -304,10 +309,11 @@ public class ActVideoDetail extends BaseUMActivity implements MediaPlayer.OnInfo
                         if (newVideo != null) {
                             user_id = newVideo.getUser_id();
                             if (TextUtils.isEmpty(video_url)) {
-                                video_url = newVideo.getResource();
+                                initVideoData(newVideo);
                                 initPlay();
+                            } else {
+                                loadVideoData(newVideo);
                             }
-                            loadVideoData(newVideo);
                         }
 //                        ActionUtil.getInstance().addNextAction(FgtArticleVideo.class.getSimpleName(), IRefresh.class.getSimpleName());
                         hideProgress();
@@ -323,12 +329,6 @@ public class ActVideoDetail extends BaseUMActivity implements MediaPlayer.OnInfo
                 });
     }
 
-    @Override
-    protected void onResume() {
-        super.onResume();
-        updateVideoCommentNo();
-    }
-
     //更新评论数量
     private void updateVideoCommentNo() {
         SheepApp.getInstance().getNetComponent().getApiService().getGameUserAppCommentList(1,1, video_id, ApiJSONUtil.COMMENT_TYPE_VIDEO)
@@ -349,74 +349,55 @@ public class ActVideoDetail extends BaseUMActivity implements MediaPlayer.OnInfo
 
     private GestureDetector mGestureDetector;
 
-    //播放或者暂停
-    private void playOrPause() {
-        if (isPlaying()) {
-            pause();
-        } else {
-            play();
-        }
-    }
     //是否正在播放视频
     private boolean isPlaying(){
-        return videoView != null && videoView.isPlaying();
+        return isPlay && !isPause;
     }
 
-    //播放
-    private void play() {
-        if (videoView == null) {
-            return;
-        }
-        videoView.start();
-    }
 
-    //暂停
-    private void pause() {
-        if (videoView == null) {
-            return;
-        }
-        videoView.pause();
+    //切换播放地址
+    protected void changeUrl() {
+        isPlay = false;
+        video_url = null;
+        video_cover_url = null;
+//        videoPlayer.setUp(getVideoUrl(), true, getVideoTitle());
     }
-
-    //销毁
-    private void destroy() {
-        if (videoView == null) {
-            return;
-        }
-        videoView.stopPlayback();
-        if(isResister)
-            SheepApp.getHttpProxyCacheServer().unregisterCacheListener(this, video_url);
+    private String getVideoUrl(){
+//        return SheepApp.getHttpProxyCacheServer().getProxyUrl(video_url);
+        return video_url;
+    }
+    private String getVideoCoverUrl(){
+        return video_cover_url;
+    }
+    private String getVideoTitle(){
+        return video_title;
     }
-
 
     @Override
-    public boolean onTouchEvent(MotionEvent event) {
-        return mGestureDetector.onTouchEvent(event);
+    protected void onResume() {
+        videoPlayer.onVideoResume();
+        super.onResume();
+        updateVideoCommentNo();
+        isPause = false;
     }
 
     @Override
-    protected void onStart() {
-        super.onStart();
-        if (!isPauseByUser)
-            play();
+    protected void onPause() {
+        videoPlayer.onVideoPause();
+        super.onPause();
+        isPause = true;
     }
 
-    //是否是用户手机暂停
-    private boolean isPauseByUser;
-
     @Override
-    protected void onStop() {
-        super.onStop();
-        if (videoView != null && videoView.isPlaying())
-            isPauseByUser = false;
-        if (video_loading != null)
-            video_loading.setVisibility(View.VISIBLE);
-        pause();
+    public void onBackPressed() {
+        //释放所有
+        videoPlayer.setVideoAllCallBack(null);
+        super.onBackPressed();
     }
 
     @Override
     protected void onDestroy() {
-        destroy();
+        GSYVideoManager.releaseAllVideos();
         super.onDestroy();
     }
 
@@ -487,10 +468,9 @@ public class ActVideoDetail extends BaseUMActivity implements MediaPlayer.OnInfo
                 LogUtil.println("mGestureListener", "Fling left", position);
                 DiscoveryVideo item = ListUtil.getItem(videoList, position);
                 if(item != null) {
+                    ViewUtil.setVisibility(video_loading, true);
                     initVideoData(item);
-                    videoView.stopPlayback();
-                    videoView.setVideoURI(getUri());
-                    videoView.requestFocus();
+                    changeUrl();
                     updateVideo();
                     updateVideoCommentNo();
                 }
@@ -505,22 +485,21 @@ public class ActVideoDetail extends BaseUMActivity implements MediaPlayer.OnInfo
                 LogUtil.println("mGestureListener", "Fling rigth", position);
                 DiscoveryVideo item = ListUtil.getItem(videoList, position);
                 if(item != null) {
+                    ViewUtil.setVisibility(video_loading, true);
                     initVideoData(item);
-                    videoView.stopPlayback();
-                    videoView.setVideoURI(getUri());
-                    videoView.requestFocus();
+                    changeUrl();
                     updateVideo();
                     updateVideoCommentNo();
                 }
             } else if (event2.getY() - event1.getY() > FLING_MIN_DISTANCE
                     && Math.abs(velocityY) > FLING_MIN_VELOCITY) {
-                if (direction == DIRECTION_LEFT_RIGHT) {
+                if (direction == DIRECTION_TOP_BOTTOM) {
                     // Fling down
                     LogUtil.println("mGestureListener", "Fling down");
                 }
             } else if (event1.getY() - event2.getY() > FLING_MIN_DISTANCE
                     && Math.abs(velocityY) > FLING_MIN_VELOCITY) {
-                if (direction == DIRECTION_LEFT_RIGHT) {
+                if (direction == DIRECTION_TOP_BOTTOM) {
                     // Fling up
                     LogUtil.println("mGestureListener", "Fling up");
                 }
@@ -530,49 +509,4 @@ public class ActVideoDetail extends BaseUMActivity implements MediaPlayer.OnInfo
         }
 
     };
-    @Override
-    public boolean onInfo(MediaPlayer mediaPlayer, int what, int extra) {
-        LogUtil.println("ActPlayVideo", "onInfo what = " + what + " extra = " + extra);
-        if(!mediaPlayer.isLooping())
-            mediaPlayer.setLooping(true);
-        switch (what) {
-            case MediaPlayer.MEDIA_INFO_BUFFERING_START:
-                LogUtil.println("ActPlayVideo", "onInfo", "正在缓冲");
-                ViewUtil.setVisibility(video_loading, true);
-                break;
-            case MediaPlayer.MEDIA_INFO_BUFFERING_END:
-            case MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START:
-                LogUtil.println("ActPlayVideo", "onInfo", "缓冲完成");
-                //缓存完成,继续播放
-                ViewUtil.setVisibility(video_loading, false);
-                ViewUtil.setVisibility(videoCoverIv, false);
-                break;
-        }
-        return false;
-    }
-
-    @Override
-    public void onPrepared(MediaPlayer mediaPlayer) {
-        LogUtil.println("ActPlayVideo", "onPrepared");
-        mediaPlayer.start();
-        ViewUtil.setVisibility(videoCoverIv, false);
-        ViewUtil.setVisibility(video_loading, false);
-    }
-
-    @Override
-    public boolean onError(MediaPlayer mediaPlayer, int what, int extra) {
-        LogUtil.println("ActPlayVideo", "onError what = " + what + " extra = " + extra);
-        initPlay();
-        return true;
-    }
-
-    @Override
-    public void onCompletion(MediaPlayer mediaPlayer) {
-        LogUtil.println("ActPlayVideo", "onCompletion");
-    }
-
-    @Override
-    public void onCacheAvailable(File cacheFile, String url, int percentsAvailable) {
-        LogUtil.println("ActPlayVideo", "onCacheAvailable", cacheFile.getAbsolutePath(), url, percentsAvailable);
-    }
 }

+ 54 - 0
app/src/main/java/com/sheep/gamegroup/view/customview/SheepGSYVideoPlayer.java

@@ -0,0 +1,54 @@
+package com.sheep.gamegroup.view.customview;
+
+import android.content.Context;
+import android.util.AttributeSet;
+import android.view.GestureDetector;
+
+import com.sheep.jiuyan.samllsheep.R;
+import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer;
+
+/**
+ * Created by realicing on 2019/1/4.
+ * realicing@sina.com
+ */
+public class SheepGSYVideoPlayer extends StandardGSYVideoPlayer {
+
+    public SheepGSYVideoPlayer(Context context, Boolean fullFlag) {
+        super(context, fullFlag);
+    }
+
+    public SheepGSYVideoPlayer(Context context) {
+        super(context);
+    }
+
+    public SheepGSYVideoPlayer(Context context, AttributeSet attrs) {
+        super(context, attrs);
+    }
+
+    @Override
+    public int getLayoutId() {
+        return R.layout.gsy_video_layout_sheep;
+    }
+    @Override
+    protected void touchSurfaceMoveFullLogic(float absDeltaX, float absDeltaY) {
+        super.touchSurfaceMoveFullLogic(absDeltaX, absDeltaY);
+        //不给触摸快进,如果需要,屏蔽下方代码即可
+//        mChangePosition = false;
+        gestureDetector = null;
+        //不给触摸音量,如果需要,屏蔽下方代码即可
+        mChangeVolume = false;
+
+        //不给触摸亮度,如果需要,屏蔽下方代码即可
+        mBrightness = false;
+    }
+    public void setGestureDetector(GestureDetector gestureDetector){
+        this.gestureDetector = gestureDetector;
+    }
+
+
+    @Override
+    protected void touchDoubleUp() {
+        //super.touchDoubleUp();
+        //不需要双击暂停
+    }
+}

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

@@ -46,6 +46,11 @@ import com.sheep.jiuyan.samllsheep.service.DownloadService;
 import com.sheep.jiuyan.samllsheep.service.FloatShotScreenService;
 import com.sheep.jiuyan.samllsheep.utils.G;
 import com.sheep.jiuyan.samllsheep.utils.SpUtils;
+import com.shuyu.gsyvideoplayer.cache.CacheFactory;
+import com.shuyu.gsyvideoplayer.cache.ProxyCacheManager;
+import com.shuyu.gsyvideoplayer.player.IjkPlayerManager;
+import com.shuyu.gsyvideoplayer.player.PlayerFactory;
+import com.shuyu.gsyvideoplayer.player.SystemPlayerManager;
 import com.squareup.leakcanary.LeakCanary;
 import com.squareup.leakcanary.RefWatcher;
 import com.tencent.bugly.Bugly;
@@ -63,6 +68,8 @@ import java.lang.ref.WeakReference;
 import cn.jpush.android.api.JPushInterface;
 import ren.yale.android.cachewebviewlib.WebViewCacheInterceptor;
 import ren.yale.android.cachewebviewlib.WebViewCacheInterceptorInst;
+import tv.danmaku.ijk.media.exo2.Exo2PlayerManager;
+import tv.danmaku.ijk.media.exo2.ExoPlayerCacheManager;
 
 /**
  * Created by kemllor on 2017/12/21.
@@ -269,6 +276,13 @@ public class SheepApp extends MultiDexApplication {
 //        RemitStoreOnSQLite.setRemitToDBDelayMillis(3000);
         //webView 缓存优化初始化 https://github.com/yale8848/CacheWebView
         WebViewCacheInterceptorInst.getInstance().init(new WebViewCacheInterceptor.Builder(this).setDebug(BuildConfig.DEBUG).setCacheSize(Long.MAX_VALUE));
+        //GSYVideoPlayer https://github.com/CarGuo/GSYVideoPlayer
+        PlayerFactory.setPlayManager(Exo2PlayerManager.class);//EXO模式
+//        PlayerFactory.setPlayManager(SystemPlayerManager.class);//系统模式
+//        PlayerFactory.setPlayManager(IjkPlayerManager.class);//ijk模式
+
+//        CacheFactory.setCacheManager(ExoPlayerCacheManager.class);//exo缓存模式,支持m3u8,只支持exo
+//        CacheFactory.setCacheManager(ProxyCacheManager.class);//代理缓存模式,支持所有模式,不支持m3u8等
 
     }
 

+ 2 - 10
app/src/main/res/layout/act_video_detail.xml

@@ -5,20 +5,12 @@
     android:layout_height="match_parent"
     android:background="#000000">
 
-    <VideoView
-        android:id="@+id/videoView"
+    <com.sheep.gamegroup.view.customview.SheepGSYVideoPlayer
+        android:id="@+id/detail_player"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         app:layout_constraintBottom_toBottomOf="parent"
         app:layout_constraintTop_toTopOf="parent" />
-    <ImageView
-        android:id="@+id/videoCoverIv"
-        android:layout_width="match_parent"
-        android:layout_height="wrap_content"
-        android:adjustViewBounds="true"
-        android:scaleType="fitXY"
-        app:layout_constraintBottom_toBottomOf="parent"
-        app:layout_constraintTop_toTopOf="parent" />
     <com.github.ybq.android.spinkit.SpinKitView
         android:id="@+id/video_loading"
         style="@style/SpinKitView.Large.ThreeBounce"

+ 29 - 0
app/src/main/res/layout/gsy_video_layout_sheep.xml

@@ -0,0 +1,29 @@
+<?xml version="1.0" encoding="utf-8"?>
+<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    android:background="@android:color/black">
+
+    <RelativeLayout
+        android:id="@+id/surface_container"
+        android:layout_width="match_parent"
+        android:layout_height="match_parent"
+        android:gravity="center">
+
+    </RelativeLayout>
+
+    <RelativeLayout
+        android:id="@+id/thumb"
+        android:layout_width="match_parent"
+        android:layout_height="match_parent"
+        android:layout_alignParentBottom="true"
+        android:layout_alignParentEnd="true"
+        android:layout_alignParentLeft="true"
+        android:layout_alignParentRight="true"
+        android:layout_alignParentStart="true"
+        android:layout_alignParentTop="true"
+        android:background="#000000"
+        android:scaleType="fitCenter" />
+
+
+</RelativeLayout>