Ver Fonte

Merge pull request #169 from Tencent/bugfix/android_scaletype

bugfix/android scaletype
hexleo há 4 anos atrás
pai
commit
94108e4fa5

+ 5 - 2
Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/AnimPlayer.kt

@@ -51,6 +51,7 @@ class AnimPlayer(val animView: IAnimView) {
     var isSurfaceAvailable = false
     var startRunnable: Runnable? = null
     var isStartRunning = false // 启动时运行状态
+    var isMute = false // 是否静音
 
     val configManager = AnimConfigManager(this)
     val pluginManager = AnimPluginManager(this)
@@ -106,11 +107,13 @@ class AnimPlayer(val animView: IAnimView) {
             if (isSurfaceAvailable) {
                 isStartRunning = false
                 decoder?.start(fileContainer)
-                audioPlayer?.start(fileContainer)
+                if (!isMute) {
+                    audioPlayer?.start(fileContainer)
+                }
             } else {
                  startRunnable = Runnable {
                     innerStartPlay(fileContainer)
-                }
+                 }
                 animView.prepareTextureView()
             }
         }

+ 41 - 17
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 {
         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 var surface: SurfaceTexture? = null
@@ -61,8 +61,7 @@ open class AnimView @JvmOverloads constructor(context: Context, attrs: Attribute
         object : IAnimListener {
 
             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)
             }
 
@@ -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 {
         hide()
@@ -100,15 +113,11 @@ open class AnimView @JvmOverloads constructor(context: Context, attrs: Attribute
 
 
     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) {
-        ALog.i(TAG, "onSurfaceTextureAvailable")
+        ALog.i(TAG, "onSurfaceTextureAvailable width=$width height=$height")
         this.surface = surface
         player.onSurfaceTextureAvailable(width, height)
     }
 
     override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
         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() {
@@ -152,7 +167,7 @@ open class AnimView @JvmOverloads constructor(context: Context, attrs: Attribute
         super.onAttachedToWindow()
         player.isDetachedFromWindow = false
         // 自动恢复播放
-        if ((player.playLoop ?: 0) > 0) {
+        if (player.playLoop > 0) {
             lastFile?.apply {
                 startPlay(this)
             }
@@ -167,6 +182,7 @@ open class AnimView @JvmOverloads constructor(context: Context, attrs: Attribute
         }
         player.isDetachedFromWindow = true
         player.onSurfaceTextureDestroyed()
+        onSizeChangedCalled = false
     }
 
 
@@ -226,6 +242,14 @@ open class AnimView @JvmOverloads constructor(context: Context, attrs: Attribute
         scaleTypeUtil.scaleTypeImpl = scaleType
     }
 
+    /**
+     * @param isMute true 静音
+     */
+    override fun setMute(isMute: Boolean) {
+        ALog.e(TAG, "set mute=$isMute")
+        player.isMute = isMute
+    }
+
     override fun startPlay(file: File) {
         try {
             val fileContainer = FileContainer(file)
@@ -257,7 +281,7 @@ open class AnimView @JvmOverloads constructor(context: Context, attrs: Attribute
                 lastFile = fileContainer
                 player.startPlay(fileContainer)
             } else {
-                ALog.i(TAG, "is running can not start")
+                ALog.e(TAG, "is running can not start")
             }
         }
     }

+ 2 - 0
Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/IAnimView.kt

@@ -50,6 +50,8 @@ interface IAnimView {
 
     fun setScaleType(scaleType: IScaleType)
 
+    fun setMute(isMute: Boolean)
+
     fun startPlay(file: File)
 
     fun startPlay(assetManager: AssetManager, assetsPath: String)

+ 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 scaleTypeFitCenter by lazy { ScaleTypeFitCenter() }
     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 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
+    }
 
     /**
      * 获取实际视频容器宽高