Procházet zdrojové kódy

视频播放器builder设置属性完善

杨充 před 4 roky
rodič
revize
ea518b34a0

+ 31 - 81
VideoPlayer/src/main/java/org/yczbj/ycvideoplayerlib/player/VideoPlayer.java

@@ -201,6 +201,16 @@ public class VideoPlayer<P extends AbstractPlayer> extends FrameLayout
         release();
     }
 
+    @Override
+    public void onWindowFocusChanged(boolean hasWindowFocus) {
+        super.onWindowFocusChanged(hasWindowFocus);
+        if (hasWindowFocus && mIsFullScreen) {
+            //重新获得焦点时保持全屏状态
+            ViewGroup decorView = VideoPlayerHelper.instance().getDecorView(mContext, mVideoController);
+            VideoPlayerHelper.instance().hideSysBar(decorView,mContext,mVideoController);
+        }
+    }
+
 
     /**
      * 初始化播放器视图
@@ -266,27 +276,12 @@ public class VideoPlayer<P extends AbstractPlayer> extends FrameLayout
      */
     protected boolean showNetWarning() {
         //播放本地数据源时不检测网络
-        if (isLocalDataSource()){
+        if (VideoPlayerHelper.instance().isLocalDataSource(mUrl,mAssetFileDescriptor)){
             return false;
         }
         return mVideoController != null && mVideoController.showNetWarning();
     }
 
-    /**
-     * 判断是否为本地数据源,包括 本地文件、Asset、raw
-     */
-    protected boolean isLocalDataSource() {
-        if (mAssetFileDescriptor != null) {
-            return true;
-        } else if (!TextUtils.isEmpty(mUrl)) {
-            Uri uri = Uri.parse(mUrl);
-            return ContentResolver.SCHEME_ANDROID_RESOURCE.equals(uri.getScheme())
-                    || ContentResolver.SCHEME_FILE.equals(uri.getScheme())
-                    || "rawresource".equals(uri.getScheme());
-        }
-        return false;
-    }
-
     /**
      * 初始化播放器
      */
@@ -302,6 +297,7 @@ public class VideoPlayer<P extends AbstractPlayer> extends FrameLayout
      * 初始化之前的配置项
      */
     protected void setInitOptions() {
+
     }
 
     /**
@@ -680,13 +676,6 @@ public class VideoPlayer<P extends AbstractPlayer> extends FrameLayout
         this.mAssetFileDescriptor = fd;
     }
 
-    /**
-     * 一开始播放就seek到预先设置好的位置
-     */
-    public void skipPositionWhenPlay(int position) {
-        this.mCurrentPosition = position;
-    }
-
     /**
      * 设置音量 0.0f-1.0f 之间
      *
@@ -758,7 +747,7 @@ public class VideoPlayer<P extends AbstractPlayer> extends FrameLayout
         }
         mIsFullScreen = true;
         //隐藏NavigationBar和StatusBar
-        hideSysBar(decorView);
+        VideoPlayerHelper.instance().hideSysBar(decorView,mContext,mVideoController);
         //从当前FrameLayout中移除播放器视图
         this.removeView(mPlayerContainer);
         //将播放器视图添加到DecorView中即实现了全屏
@@ -766,29 +755,6 @@ public class VideoPlayer<P extends AbstractPlayer> extends FrameLayout
         setPlayerState(ConstantKeys.PlayMode.MODE_FULL_SCREEN);
     }
 
-    private void hideSysBar(ViewGroup decorView) {
-        int uiOptions = decorView.getSystemUiVisibility();
-        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
-            uiOptions |= View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
-        }
-        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
-            uiOptions |= View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
-        }
-        decorView.setSystemUiVisibility(uiOptions);
-        VideoPlayerHelper.instance().getActivity(mContext,mVideoController).getWindow().setFlags(
-                WindowManager.LayoutParams.FLAG_FULLSCREEN,
-                WindowManager.LayoutParams.FLAG_FULLSCREEN);
-    }
-
-    @Override
-    public void onWindowFocusChanged(boolean hasWindowFocus) {
-        super.onWindowFocusChanged(hasWindowFocus);
-        if (hasWindowFocus && mIsFullScreen) {
-            //重新获得焦点时保持全屏状态
-            hideSysBar(VideoPlayerHelper.instance().getDecorView(mContext,mVideoController));
-        }
-    }
-
     /**
      * 退出全屏
      */
@@ -798,13 +764,12 @@ public class VideoPlayer<P extends AbstractPlayer> extends FrameLayout
             return;
         }
         ViewGroup decorView = VideoPlayerHelper.instance().getDecorView(mContext,mVideoController);
-        if (decorView == null)
+        if (decorView == null){
             return;
-
+        }
         mIsFullScreen = false;
-
         //显示NavigationBar和StatusBar
-        showSysBar(decorView);
+        VideoPlayerHelper.instance().showSysBar(decorView,mContext,mVideoController);
 
         //把播放器视图从DecorView中移除并添加到当前FrameLayout中即退出了全屏
         decorView.removeView(mPlayerContainer);
@@ -813,18 +778,6 @@ public class VideoPlayer<P extends AbstractPlayer> extends FrameLayout
         setPlayerState(ConstantKeys.PlayMode.MODE_NORMAL);
     }
 
-    private void showSysBar(ViewGroup decorView) {
-        int uiOptions = decorView.getSystemUiVisibility();
-        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
-            uiOptions &= ~View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
-        }
-        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
-            uiOptions &= ~View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
-        }
-        decorView.setSystemUiVisibility(uiOptions);
-        VideoPlayerHelper.instance().getActivity(mContext,mVideoController)
-                .getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
-    }
 
 
     /**
@@ -958,15 +911,6 @@ public class VideoPlayer<P extends AbstractPlayer> extends FrameLayout
         }
     }
 
-    /**
-     * 设置小屏的宽高
-     *
-     * @param tinyScreenSize 其中tinyScreenSize[0]是宽,tinyScreenSize[1]是高
-     */
-    public void setTinyScreenSize(int[] tinyScreenSize) {
-        this.mTinyScreenSize = tinyScreenSize;
-    }
-
     /**
      * 向Controller设置播放状态,用于控制Controller的ui展示
      */
@@ -1071,17 +1015,23 @@ public class VideoPlayer<P extends AbstractPlayer> extends FrameLayout
     /**-----------------------------暴露api方法--------------------------------------**/
     /**-----------------------------暴露api方法--------------------------------------**/
 
-    /**
-     * 设置视频播放器的背景色
-     */
-    public void setPlayerBackgroundColor(@ColorInt int color) {
-        //使用注解限定福
-        if (color==0){
-            mPlayerContainer.setBackgroundColor(Color.BLACK);
-        } else {
-            mPlayerContainer.setBackgroundColor(color);
+
+    public void setVideoBuilder(VideoPlayerBuilder videoBuilder){
+        if (mPlayerContainer==null){
+            return;
+        }
+        //设置视频播放器的背景色
+        mPlayerContainer.setBackgroundColor(videoBuilder.mColor);
+        //设置小屏的宽高
+        if (videoBuilder.mTinyScreenSize.length>0){
+            mTinyScreenSize = videoBuilder.mTinyScreenSize;
+        }
+        //一开始播放就seek到预先设置好的位置
+        if (videoBuilder.mCurrentPosition>0){
+            this.mCurrentPosition = videoBuilder.mCurrentPosition;
         }
     }
 
 
+
 }

+ 78 - 0
VideoPlayer/src/main/java/org/yczbj/ycvideoplayerlib/player/VideoPlayerBuilder.java

@@ -0,0 +1,78 @@
+package org.yczbj.ycvideoplayerlib.player;
+
+import android.graphics.Color;
+
+import androidx.annotation.ColorInt;
+
+import com.yc.kernel.impl.media.MediaPlayerFactory;
+
+import org.yczbj.ycvideoplayerlib.config.PlayerConfig;
+import org.yczbj.ycvideoplayerlib.surface.TextureViewFactory;
+
+public class VideoPlayerBuilder {
+
+    public static Builder newBuilder() {
+        return new Builder();
+    }
+
+    public final static class Builder {
+
+        private int mColor = 0;
+        private int[] mTinyScreenSize;
+        private int mCurrentPosition = -1;
+
+        /**
+         * 设置视频播放器的背景色
+         * @param color                         color
+         * @return                              Builder
+         */
+        public Builder setPlayerBackgroundColor(@ColorInt int color) {
+            //使用注解限定福
+            if (color==0){
+                this.mColor = Color.BLACK;
+            } else {
+                this.mColor = color;
+            }
+            return this;
+        }
+
+        /**
+         * 设置小屏的宽高
+         * @param tinyScreenSize                其中tinyScreenSize[0]是宽,tinyScreenSize[1]是高
+         * @return                              Builder
+         */
+        public Builder setTinyScreenSize(int[] tinyScreenSize) {
+            this.mTinyScreenSize = tinyScreenSize;
+            return this;
+        }
+
+        /**
+         * 一开始播放就seek到预先设置好的位置
+         * @param position                      位置
+         * @return                              Builder
+         */
+        public Builder skipPositionWhenPlay(int position) {
+            this.mCurrentPosition = position;
+            return this;
+        }
+
+
+        public VideoPlayerBuilder build() {
+            //创建builder对象
+            return new VideoPlayerBuilder(this);
+        }
+    }
+
+
+    public final int mColor;
+    public final int[] mTinyScreenSize;
+    public final int mCurrentPosition;
+
+    public VideoPlayerBuilder(Builder builder) {
+        mColor = builder.mColor;
+        mTinyScreenSize = builder.mTinyScreenSize;
+        mCurrentPosition = builder.mCurrentPosition;
+    }
+
+
+}

+ 67 - 0
VideoPlayer/src/main/java/org/yczbj/ycvideoplayerlib/player/VideoPlayerHelper.java

@@ -1,8 +1,16 @@
 package org.yczbj.ycvideoplayerlib.player;
 
 import android.app.Activity;
+import android.content.ContentResolver;
 import android.content.Context;
+import android.content.res.AssetFileDescriptor;
+import android.net.Uri;
+import android.os.Build;
+import android.text.TextUtils;
+import android.view.View;
 import android.view.ViewGroup;
+import android.view.WindowManager;
+
 import org.yczbj.ycvideoplayerlib.controller.BaseVideoController;
 import org.yczbj.ycvideoplayerlib.tool.utils.PlayerUtils;
 
@@ -80,5 +88,64 @@ public class VideoPlayerHelper {
         return activity;
     }
 
+    /**
+     * 显示NavigationBar和StatusBar
+     * @param decorView                             decorView
+     * @param context                               上下文
+     * @param videoController                       controller
+     */
+    protected void showSysBar(ViewGroup decorView,Context context , BaseVideoController videoController) {
+        int uiOptions = decorView.getSystemUiVisibility();
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
+            uiOptions &= ~View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
+        }
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
+            uiOptions &= ~View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
+        }
+        decorView.setSystemUiVisibility(uiOptions);
+        VideoPlayerHelper.instance().getActivity(context,videoController)
+                .getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
+    }
+
+    /**
+     * 隐藏NavigationBar和StatusBar
+     * @param decorView                             decorView
+     * @param context                               上下文
+     * @param videoController                       controller
+     */
+    protected void hideSysBar(ViewGroup decorView,Context context , BaseVideoController videoController) {
+        int uiOptions = decorView.getSystemUiVisibility();
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
+            uiOptions |= View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
+        }
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
+            uiOptions |= View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
+        }
+        decorView.setSystemUiVisibility(uiOptions);
+        VideoPlayerHelper.instance().getActivity(context,videoController).getWindow().setFlags(
+                WindowManager.LayoutParams.FLAG_FULLSCREEN,
+                WindowManager.LayoutParams.FLAG_FULLSCREEN);
+    }
+
+
+    /**
+     * 判断是否为本地数据源,包括 本地文件、Asset、raw
+     * @param url                                   url地址
+     * @param assetFileDescriptor                   assets文件
+     * @return                                      是否为本地数据源
+     */
+    protected boolean isLocalDataSource(String url, AssetFileDescriptor assetFileDescriptor) {
+        if (assetFileDescriptor != null) {
+            return true;
+        } else if (!TextUtils.isEmpty(url)) {
+            Uri uri = Uri.parse(url);
+            return ContentResolver.SCHEME_ANDROID_RESOURCE.equals(uri.getScheme())
+                    || ContentResolver.SCHEME_FILE.equals(uri.getScheme())
+                    || "rawresource".equals(uri.getScheme());
+        }
+        return false;
+    }
+
+
 
 }

+ 8 - 1
app/src/main/java/org/yczbj/ycvideoplayer/activity/NormalActivity.java

@@ -14,6 +14,7 @@ import org.yczbj.ycvideoplayer.ConstantVideo;
 import org.yczbj.ycvideoplayer.R;
 import org.yczbj.ycvideoplayerlib.config.ConstantKeys;
 import org.yczbj.ycvideoplayerlib.player.VideoPlayer;
+import org.yczbj.ycvideoplayerlib.player.VideoPlayerBuilder;
 import org.yczbj.ycvideoplayerlib.ui.view.BasisVideoController;
 
 public class NormalActivity extends AppCompatActivity implements View.OnClickListener {
@@ -111,8 +112,14 @@ public class NormalActivity extends AppCompatActivity implements View.OnClickLis
 
     private void test(){
         //VideoPlayer相关
+        VideoPlayerBuilder.Builder builder = VideoPlayerBuilder.newBuilder();
+        VideoPlayerBuilder videoPlayerBuilder = new VideoPlayerBuilder(builder);
         //设置视频播放器的背景色
-        mVideoPlayer.setPlayerBackgroundColor(Color.BLACK);
+        builder.setPlayerBackgroundColor(Color.BLACK);
+        //设置小屏的宽高
+        int[] mTinyScreenSize = {0, 0};
+        builder.setTinyScreenSize(mTinyScreenSize);
+        mVideoPlayer.setVideoBuilder(videoPlayerBuilder);
     }
 
 }

+ 6 - 0
read/09.视频播放器使用设计模式.md

@@ -1,12 +1,18 @@
 #### 面向对象优化介绍
 - 01.面向对象6大原则介绍
 - 02.单一指责的演变和迭代
+- 03.接口过于庞大而分离
 - 07.视频状态View的演变
 
 
 
 
 
+### 03.接口过于庞大而分离
+- 比如举个例子
+
+
+
 
 
 ### 07.视频状态View的演变