Skip to content

Commit

Permalink
Merge pull request #169 from Tencent/bugfix/android_scaletype
Browse files Browse the repository at this point in the history
bugfix/android scaletype
  • Loading branch information
hexleo authored Nov 10, 2021
2 parents 17a1a84 + cf7b3f5 commit 94108e4
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}

Expand Down Expand Up @@ -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()
Expand All @@ -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
}
}

Expand Down Expand Up @@ -136,23 +145,29 @@ 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() {
ALog.i(TAG, "onAttachedToWindow")
super.onAttachedToWindow()
player.isDetachedFromWindow = false
// 自动恢复播放
if ((player.playLoop ?: 0) > 0) {
if (player.playLoop > 0) {
lastFile?.apply {
startPlay(this)
}
Expand All @@ -167,6 +182,7 @@ open class AnimView @JvmOverloads constructor(context: Context, attrs: Attribute
}
player.isDetachedFromWindow = true
player.onSurfaceTextureDestroyed()
onSizeChangedCalled = false
}


Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ interface IAnimView {

fun setScaleType(scaleType: IScaleType)

fun setMute(isMute: Boolean)

fun startPlay(file: File)

fun startPlay(assetManager: AssetManager, assetsPath: String)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

/**
* 获取实际视频容器宽高
Expand Down

0 comments on commit 94108e4

Please sign in to comment.