Kaynağa Gözat

bug: 解决在onCreate中调用startPlay导致视频对齐方式错误问题 close #164

hexleo 4 yıl önce
ebeveyn
işleme
e151ec9a1b

+ 32 - 16
Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/AnimView.kt

@@ -47,7 +47,7 @@ open class AnimView @JvmOverloads constructor(context: Context, attrs: Attribute
     companion object {
     companion object {
         private const val TAG = "${Constant.TAG}.AnimView"
         private const val TAG = "${Constant.TAG}.AnimView"
     }
     }
-    private val player: AnimPlayer
+    private lateinit var player: AnimPlayer
 
 
     private val uiHandler by lazy { Handler(Looper.getMainLooper()) }
     private val uiHandler by lazy { Handler(Looper.getMainLooper()) }
     private var surface: SurfaceTexture? = null
     private var surface: SurfaceTexture? = null
@@ -61,8 +61,7 @@ open class AnimView @JvmOverloads constructor(context: Context, attrs: Attribute
         object : IAnimListener {
         object : IAnimListener {
 
 
             override fun onVideoConfigReady(config: AnimConfig): Boolean {
             override fun onVideoConfigReady(config: AnimConfig): Boolean {
-                scaleTypeUtil.videoWidth = config.width
-                scaleTypeUtil.videoHeight = config.height
+                scaleTypeUtil.setVideoSize(config.width, config.height)
                 return animListener?.onVideoConfigReady(config) ?: super.onVideoConfigReady(config)
                 return animListener?.onVideoConfigReady(config) ?: super.onVideoConfigReady(config)
             }
             }
 
 
@@ -91,6 +90,20 @@ open class AnimView @JvmOverloads constructor(context: Context, attrs: Attribute
         }
         }
     }
     }
 
 
+    // 保证AnimView已经布局完成才加入TextureView
+    private var onSizeChangedCalled = false
+    private var needPrepareTextureView = false
+    private val prepareTextureViewRunnable = Runnable {
+        removeAllViews()
+        innerTextureView = InnerTextureView(context).apply {
+            player = this@AnimView.player
+            isOpaque = false
+            surfaceTextureListener = this@AnimView
+            layoutParams = scaleTypeUtil.getLayoutParam(this)
+        }
+        addView(innerTextureView)
+    }
+
 
 
     init {
     init {
         hide()
         hide()
@@ -100,15 +113,11 @@ open class AnimView @JvmOverloads constructor(context: Context, attrs: Attribute
 
 
 
 
     override fun prepareTextureView() {
     override fun prepareTextureView() {
-        uiHandler.post {
-            removeAllViews()
-            innerTextureView = InnerTextureView(context).apply {
-                player = this@AnimView.player
-                isOpaque = false
-                surfaceTextureListener = this@AnimView
-                layoutParams = scaleTypeUtil.getLayoutParam(this)
-            }
-            addView(innerTextureView)
+        if (onSizeChangedCalled) {
+            uiHandler.post(prepareTextureViewRunnable)
+        } else {
+            ALog.e(TAG, "onSizeChanged not Called")
+            needPrepareTextureView = true
         }
         }
     }
     }
 
 
@@ -136,15 +145,21 @@ open class AnimView @JvmOverloads constructor(context: Context, attrs: Attribute
     }
     }
 
 
     override fun onSurfaceTextureAvailable(surface: SurfaceTexture, width: Int, height: Int) {
     override fun onSurfaceTextureAvailable(surface: SurfaceTexture, width: Int, height: Int) {
-        ALog.i(TAG, "onSurfaceTextureAvailable")
+        ALog.i(TAG, "onSurfaceTextureAvailable width=$width height=$height")
         this.surface = surface
         this.surface = surface
         player.onSurfaceTextureAvailable(width, height)
         player.onSurfaceTextureAvailable(width, height)
     }
     }
 
 
     override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
     override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
         super.onSizeChanged(w, h, oldw, oldh)
         super.onSizeChanged(w, h, oldw, oldh)
-        scaleTypeUtil.layoutWidth = w
-        scaleTypeUtil.layoutHeight = h
+        ALog.i(TAG, "onSizeChanged w=$w, h=$h")
+        scaleTypeUtil.setLayoutSize(w, h)
+        onSizeChangedCalled = true
+        // 需要保证onSizeChanged被调用
+        if (needPrepareTextureView) {
+            needPrepareTextureView = false
+            prepareTextureView()
+        }
     }
     }
 
 
     override fun onAttachedToWindow() {
     override fun onAttachedToWindow() {
@@ -152,7 +167,7 @@ open class AnimView @JvmOverloads constructor(context: Context, attrs: Attribute
         super.onAttachedToWindow()
         super.onAttachedToWindow()
         player.isDetachedFromWindow = false
         player.isDetachedFromWindow = false
         // 自动恢复播放
         // 自动恢复播放
-        if ((player.playLoop ?: 0) > 0) {
+        if (player.playLoop > 0) {
             lastFile?.apply {
             lastFile?.apply {
                 startPlay(this)
                 startPlay(this)
             }
             }
@@ -167,6 +182,7 @@ open class AnimView @JvmOverloads constructor(context: Context, attrs: Attribute
         }
         }
         player.isDetachedFromWindow = true
         player.isDetachedFromWindow = true
         player.onSurfaceTextureDestroyed()
         player.onSurfaceTextureDestroyed()
+        onSizeChangedCalled = false
     }
     }
 
 
 
 

+ 12 - 5
Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/util/ScaleTypeUtil.kt

@@ -175,16 +175,23 @@ class ScaleTypeUtil {
     private val scaleTypeFitXY by lazy { ScaleTypeFitXY() }
     private val scaleTypeFitXY by lazy { ScaleTypeFitXY() }
     private val scaleTypeFitCenter by lazy { ScaleTypeFitCenter() }
     private val scaleTypeFitCenter by lazy { ScaleTypeFitCenter() }
     private val scaleTypeCenterCrop by lazy { ScaleTypeCenterCrop() }
     private val scaleTypeCenterCrop by lazy { ScaleTypeCenterCrop() }
+    private var layoutWidth = 0
+    private var layoutHeight = 0
+    private var videoWidth = 0
+    private var videoHeight = 0
 
 
     var currentScaleType = ScaleType.FIT_XY
     var currentScaleType = ScaleType.FIT_XY
     var scaleTypeImpl: IScaleType? = null
     var scaleTypeImpl: IScaleType? = null
 
 
-    var layoutWidth = 0
-    var layoutHeight = 0
-
-    var videoWidth = 0
-    var videoHeight = 0
+    fun setLayoutSize(w: Int, h: Int) {
+        layoutWidth = w
+        layoutHeight = h
+    }
 
 
+    fun setVideoSize(w: Int, h: Int) {
+        videoWidth = w
+        videoHeight = h
+    }
 
 
     /**
     /**
      * 获取实际视频容器宽高
      * 获取实际视频容器宽高