Explorar o código

优化VideoTextureView代码

yangchong211 %!s(int64=6) %!d(string=hai) anos
pai
achega
43c3031d1a

+ 1 - 1
LICENSE

@@ -186,7 +186,7 @@
       same "printed page" as the copyright notice for easier
       identification within third-party archives.
 
-   Copyright [yyyy] [name of copyright owner]
+   Copyright [2016] [杨充  email:yangchong211@163.com]
 
    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.

+ 25 - 0
YCVideoPlayerLib/src/main/java/org/yczbj/ycvideoplayerlib/inter/listener/OnSurfaceListener.java

@@ -0,0 +1,25 @@
+package org.yczbj.ycvideoplayerlib.inter.listener;
+
+
+import android.graphics.SurfaceTexture;
+
+/**
+ * <pre>
+ *     @author yangchong
+ *     blog  : https://github.com/yangchong211
+ *     time  : 2017/11/9
+ *     desc  : VideoTextureView监听
+ *     revise:
+ * </pre>
+ */
+public interface OnSurfaceListener {
+
+    void onSurfaceAvailable(SurfaceTexture surface);
+
+    void onSurfaceSizeChanged(SurfaceTexture surface, int width, int height);
+
+    boolean onSurfaceDestroyed(SurfaceTexture surface);
+
+    void onSurfaceUpdated(SurfaceTexture surface);
+
+}

+ 8 - 23
YCVideoPlayerLib/src/main/java/org/yczbj/ycvideoplayerlib/player/VideoPlayer.java

@@ -22,14 +22,13 @@ import android.widget.Toast;
 import org.yczbj.ycvideoplayerlib.constant.ConstantKeys;
 import org.yczbj.ycvideoplayerlib.controller.AbsVideoPlayerController;
 import org.yczbj.ycvideoplayerlib.inter.InterVideoPlayer;
+import org.yczbj.ycvideoplayerlib.inter.listener.OnSurfaceListener;
 import org.yczbj.ycvideoplayerlib.manager.VideoPlayerManager;
 import org.yczbj.ycvideoplayerlib.utils.VideoLogUtil;
 import org.yczbj.ycvideoplayerlib.utils.VideoPlayerUtils;
 import org.yczbj.ycvideoplayerlib.view.VideoTextureView;
-
 import java.io.IOException;
 import java.util.Map;
-
 import tv.danmaku.ijk.media.player.AndroidMediaPlayer;
 import tv.danmaku.ijk.media.player.IMediaPlayer;
 import tv.danmaku.ijk.media.player.IjkMediaPlayer;
@@ -232,7 +231,6 @@ public class VideoPlayer extends FrameLayout implements InterVideoPlayer {
             initAudioManager();
             initMediaPlayer();
             initTextureView();
-            addTextureView();
         } else {
             VideoLogUtil.d("VideoPlayer只有在mCurrentState == STATE_IDLE时才能调用start方法.");
         }
@@ -639,14 +637,13 @@ public class VideoPlayer extends FrameLayout implements InterVideoPlayer {
     @RequiresApi(api = Build.VERSION_CODES.ICE_CREAM_SANDWICH)
     private void initTextureView() {
         if (mTextureView == null) {
-            mTextureView = new VideoTextureView(mContext);
-            mTextureView.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() {
+            mTextureView = VideoTextureView.addTextureView(mContext, new OnSurfaceListener() {
                 /**
                  * SurfaceTexture准备就绪
                  */
                 @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
                 @Override
-                public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
+                public void onSurfaceAvailable(SurfaceTexture surface) {
                     if (mSurfaceTexture == null) {
                         mSurfaceTexture = surface;
                         openMediaPlayer();
@@ -659,7 +656,7 @@ public class VideoPlayer extends FrameLayout implements InterVideoPlayer {
                  * SurfaceTexture缓冲大小变化
                  */
                 @Override
-                public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
+                public void onSurfaceSizeChanged(SurfaceTexture surface, int width, int height) {
 
                 }
 
@@ -667,33 +664,22 @@ public class VideoPlayer extends FrameLayout implements InterVideoPlayer {
                  * SurfaceTexture即将被销毁
                  */
                 @Override
-                public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
-                    return mSurfaceTexture == null;
+                public boolean onSurfaceDestroyed(SurfaceTexture surface) {
+                    return false;
                 }
 
                 /**
                  * SurfaceTexture通过updateImage更新
                  */
                 @Override
-                public void onSurfaceTextureUpdated(SurfaceTexture surface) {
+                public void onSurfaceUpdated(SurfaceTexture surface) {
 
                 }
-            });
+            },mContainer);
         }
     }
 
 
-    /**
-     * 添加TextureView到视图中
-     */
-    private void addTextureView() {
-        mContainer.removeView(mTextureView);
-        LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
-                ViewGroup.LayoutParams.MATCH_PARENT, Gravity.CENTER);
-        mContainer.addView(mTextureView, 0, params);
-    }
-
-
     /**
      * 打开MediaPlayer播放器
      */
@@ -746,7 +732,6 @@ public class VideoPlayer extends FrameLayout implements InterVideoPlayer {
         }
     }
 
-
     /**
      * 设置准备视频播放监听事件
      */

+ 0 - 132
YCVideoPlayerLib/src/main/java/org/yczbj/ycvideoplayerlib/view/VideoSurfaceView.java

@@ -1,132 +0,0 @@
-package org.yczbj.ycvideoplayerlib.view;
-
-import android.content.Context;
-import android.util.AttributeSet;
-import android.view.SurfaceView;
-
-
-/**
- * <pre>
- *     @author yangchong
- *     blog  : https://github.com/yangchong211
- *     time  : 2017/10/21
- *     desc  : 重写SurfaceView,适配视频的宽高和旋转
- *     revise:
- * </pre>
- */
-public class VideoSurfaceView extends SurfaceView {
-
-    private int videoHeight;
-    private int videoWidth;
-
-    public VideoSurfaceView(Context context) {
-        super(context);
-    }
-
-    public VideoSurfaceView(Context context, AttributeSet attrs) {
-        super(context, attrs);
-    }
-
-    public VideoSurfaceView(Context context, AttributeSet attrs, int defStyleAttr) {
-        super(context, attrs, defStyleAttr);
-    }
-
-
-    /**
-     * 自定义video大小
-     * @param videoWidth                宽
-     * @param videoHeight               高
-     */
-    public void adaptVideoSize(int videoWidth, int videoHeight) {
-        if (this.videoWidth != videoWidth && this.videoHeight != videoHeight) {
-            this.videoWidth = videoWidth;
-            this.videoHeight = videoHeight;
-            requestLayout();
-        }
-    }
-
-    /**
-     * 记得一定要重新写这个方法,如果角度发生了变化,就重新绘制布局
-     * 设置视频旋转角度
-     * @param rotation                  角度
-     */
-    @Override
-    public void setRotation(float rotation) {
-        if (rotation != getRotation()) {
-            super.setRotation(rotation);
-            requestLayout();
-        }
-    }
-
-    @Override
-    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
-        float viewRotation = getRotation();
-        // 如果判断成立,则说明显示的TextureView和本身的位置是有90度的旋转的,所以需要交换宽高参数。
-        float viewRotation1 = 90f;
-        float viewRotation2 = 270f;
-        if (viewRotation == viewRotation1 || viewRotation == viewRotation2) {
-            int tempMeasureSpec = widthMeasureSpec;
-            //noinspection SuspiciousNameCombination
-            widthMeasureSpec = heightMeasureSpec;
-            heightMeasureSpec = tempMeasureSpec;
-        }
-
-        int width = getDefaultSize(videoWidth, widthMeasureSpec);
-        int height = getDefaultSize(videoHeight, heightMeasureSpec);
-        if (videoWidth > 0 && videoHeight > 0) {
-
-            int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
-            int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
-            int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
-            int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
-
-            if (widthSpecMode == MeasureSpec.EXACTLY && heightSpecMode == MeasureSpec.EXACTLY) {
-                // the size is fixed
-                width = widthSpecSize;
-                height = heightSpecSize;
-                // for compatibility, we adjust size based on aspect ratio
-                if (videoWidth * height < width * videoHeight) {
-                    width = height * videoWidth / videoHeight;
-                } else if (videoWidth * height > width * videoHeight) {
-                    height = width * videoHeight / videoWidth;
-                }
-            } else if (widthSpecMode == MeasureSpec.EXACTLY) {
-                // only the width is fixed, adjust the height to match aspect ratio if possible
-                width = widthSpecSize;
-                height = width * videoHeight / videoWidth;
-                if (heightSpecMode == MeasureSpec.AT_MOST && height > heightSpecSize) {
-                    // couldn't match aspect ratio within the constraints
-                    height = heightSpecSize;
-                    width = height * videoWidth / videoHeight;
-                }
-            } else if (heightSpecMode == MeasureSpec.EXACTLY) {
-                // only the height is fixed, adjust the width to match aspect ratio if possible
-                height = heightSpecSize;
-                width = height * videoWidth / videoHeight;
-                if (widthSpecMode == MeasureSpec.AT_MOST && width > widthSpecSize) {
-                    // couldn't match aspect ratio within the constraints
-                    width = widthSpecSize;
-                    height = width * videoHeight / videoWidth;
-                }
-            } else {
-                // neither the width nor the height are fixed, try to use actual video size
-                width = videoWidth;
-                height = videoHeight;
-                if (heightSpecMode == MeasureSpec.AT_MOST && height > heightSpecSize) {
-                    // too tall, decrease both width and height
-                    height = heightSpecSize;
-                    width = height * videoWidth / videoHeight;
-                }
-                if (widthSpecMode == MeasureSpec.AT_MOST && width > widthSpecSize) {
-                    // too wide, decrease both width and height
-                    width = widthSpecSize;
-                    height = width * videoHeight / videoWidth;
-                }
-            }
-        } else {
-            // no size yet, just adopt the given spec sizes
-        }
-        setMeasuredDimension(width, height);
-    }
-
-}

+ 108 - 3
YCVideoPlayerLib/src/main/java/org/yczbj/ycvideoplayerlib/view/VideoTextureView.java

@@ -2,9 +2,13 @@ package org.yczbj.ycvideoplayerlib.view;
 
 import android.annotation.SuppressLint;
 import android.content.Context;
-import android.os.Build;
-import android.support.annotation.RequiresApi;
+import android.graphics.SurfaceTexture;
+import android.view.Gravity;
 import android.view.TextureView;
+import android.view.ViewGroup;
+import android.widget.FrameLayout;
+
+import org.yczbj.ycvideoplayerlib.inter.listener.OnSurfaceListener;
 
 
 /**
@@ -17,15 +21,114 @@ import android.view.TextureView;
  * </pre>
  */
 @SuppressLint("NewApi")
-public class VideoTextureView extends TextureView {
+public class VideoTextureView extends TextureView implements TextureView.SurfaceTextureListener {
 
     private int videoHeight;
     private int videoWidth;
+    private OnSurfaceListener onSurfaceListener;
+
 
     public VideoTextureView(Context context) {
         super(context);
     }
 
+
+    /**
+     * SurfaceTexture准备就绪
+     * @param surface                   surface
+     * @param width                     width
+     * @param height                    height
+     */
+    @Override
+    public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
+        if (onSurfaceListener != null) {
+            onSurfaceListener.onSurfaceAvailable(surface);
+        }
+    }
+
+
+    /**
+     * SurfaceTexture缓冲大小变化
+     * @param surface                   surface
+     * @param width                     width
+     * @param height                    height
+     */
+    @Override
+    public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
+        if (onSurfaceListener != null) {
+            onSurfaceListener.onSurfaceSizeChanged(surface, width, height);
+        }
+    }
+
+
+    /**
+     * SurfaceTexture即将被销毁
+     * @param surface                   surface
+     */
+    @Override
+    public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
+        //清空释放
+        if (onSurfaceListener != null) {
+            onSurfaceListener.onSurfaceDestroyed(surface);
+        }
+        return true;
+    }
+
+
+    /**
+     * SurfaceTexture通过updateImage更新
+     * @param surface                   surface
+     */
+    @Override
+    public void onSurfaceTextureUpdated(SurfaceTexture surface) {
+        //如果播放的是暂停全屏了
+        if (onSurfaceListener != null) {
+            onSurfaceListener.onSurfaceUpdated(surface);
+        }
+    }
+
+
+    /**
+     * 获取listener
+     * @return                          onSurfaceListener
+     */
+    public OnSurfaceListener getOnSurfaceListener() {
+        return onSurfaceListener;
+    }
+
+
+    /**
+     * 设置监听
+     * @param surfaceListener           onSurfaceListener
+     */
+    public void setOnSurfaceListener(OnSurfaceListener surfaceListener) {
+        setSurfaceTextureListener(this);
+        onSurfaceListener = surfaceListener;
+    }
+
+
+    /**
+     * 添加TextureView到视图中
+     * @param context                   上下文
+     * @param listener                  listener监听
+     * @param frameLayout               父布局
+     * @return                          VideoTextureView
+     */
+    public static VideoTextureView addTextureView(Context context , OnSurfaceListener listener ,
+                                                  FrameLayout frameLayout){
+        VideoTextureView videoTextureView = new VideoTextureView(context);
+        videoTextureView.setOnSurfaceListener(listener);
+        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
+                ViewGroup.LayoutParams.MATCH_PARENT,
+                ViewGroup.LayoutParams.MATCH_PARENT, Gravity.CENTER);
+        //防止多次添加,所以每次先移除,后添加
+        frameLayout.removeView(videoTextureView);
+        //添加到视图中
+        frameLayout.addView(videoTextureView, 0, params);
+        return videoTextureView;
+    }
+
+
     /**
      * 自定义video大小
      * @param videoWidth                宽
@@ -39,6 +142,7 @@ public class VideoTextureView extends TextureView {
         }
     }
 
+
     /**
      * 记得一定要重新写这个方法,如果角度发生了变化,就重新绘制布局
      * 设置视频旋转角度
@@ -52,6 +156,7 @@ public class VideoTextureView extends TextureView {
         }
     }
 
+
     @Override
     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
         float viewRotation = getRotation();