Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加最后一帧停止的功能。code cleanup。update agp to 7.3.0 #265

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions Android/PlayerProj/animplayer/build.gradle
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
compileSdkVersion 28
compileSdkVersion 33

defaultConfig {
minSdkVersion 16
targetSdkVersion 28
versionCode 1
versionName "1.0"
targetSdkVersion 33
}

buildTypes {
Expand All @@ -18,13 +15,14 @@ android {
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
buildFeatures {
viewBinding true
}
namespace 'com.tencent.qgame.animplayer'

}

dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version") {
exclude module: 'annotations'
}
}

// jcenter 上传(这个要在底部)
Expand Down
3 changes: 1 addition & 2 deletions Android/PlayerProj/animplayer/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tencent.qgame.animplayer"/>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" />
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
*/
package com.tencent.qgame.animplayer

import android.graphics.Bitmap
import android.graphics.MaskFilter
import com.tencent.qgame.animplayer.mask.MaskConfig
import com.tencent.qgame.animplayer.util.ALog
import org.json.JSONException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ class AnimConfigManager(val player: AnimPlayer) {
private fun parseBoxHead(boxHead: ByteArray): BoxHead? {
if (boxHead.size != 8) return null
val head = BoxHead()
var length: Int = 0
var length = 0
length = length or (boxHead[0].toInt() and 0xff shl 24)
length = length or (boxHead[1].toInt() and 0xff shl 16)
length = length or (boxHead[2].toInt() and 0xff shl 8)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class AnimPlayer(val animView: IAnimView) {
private const val TAG = "${Constant.TAG}.AnimPlayer"
}

var autoDismiss = true
var animListener: IAnimListener? = null
var decoder: Decoder? = null
var audioPlayer: AudioPlayer? = null
Expand All @@ -39,7 +40,6 @@ class AnimPlayer(val animView: IAnimView) {
var defaultFps: Int = 0
var playLoop: Int = 0
set(value) {
decoder?.playLoop = value
audioPlayer?.playLoop = value
field = value
}
Expand Down Expand Up @@ -136,6 +136,7 @@ class AnimPlayer(val animView: IAnimView) {
private fun prepareDecoder() {
if (decoder == null) {
decoder = HardDecoder(this).apply {
autoDismiss = [email protected]
playLoop = [email protected]
fps = [email protected]
}
Expand All @@ -148,10 +149,11 @@ class AnimPlayer(val animView: IAnimView) {
}

fun updateMaskConfig(maskConfig: MaskConfig?) {
configManager.config?.maskConfig = configManager.config?.maskConfig ?: MaskConfig()
configManager.config?.maskConfig?.safeSetMaskBitmapAndReleasePre(maskConfig?.alphaMaskBitmap)
configManager.config?.maskConfig?.maskPositionPair = maskConfig?.maskPositionPair
configManager.config?.maskConfig?.maskTexPair = maskConfig?.maskTexPair
val config = configManager.config?.maskConfig ?: MaskConfig()
configManager.config?.maskConfig = config
config.safeSetMaskBitmapAndReleasePre(maskConfig?.alphaMaskBitmap)
config.maskPositionPair = maskConfig?.maskPositionPair
config.maskTexPair = maskConfig?.maskTexPair
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package com.tencent.qgame.animplayer
import android.content.Context
import android.content.res.AssetManager
import android.graphics.SurfaceTexture
import android.os.Build
import android.os.Handler
import android.os.Looper
import android.util.AttributeSet
Expand All @@ -39,22 +38,28 @@ import com.tencent.qgame.animplayer.util.ScaleType
import com.tencent.qgame.animplayer.util.ScaleTypeUtil
import java.io.File

open class AnimView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0):
class AnimView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) :
IAnimView,
FrameLayout(context, attrs, defStyleAttr),
TextureView.SurfaceTextureListener {

companion object {
private const val TAG = "${Constant.TAG}.AnimView"
}
private lateinit var player: AnimPlayer

private val player: AnimPlayer

private val uiHandler by lazy { Handler(Looper.getMainLooper()) }
private var surface: SurfaceTexture? = null
private var animListener: IAnimListener? = null
private var innerTextureView: InnerTextureView? = null
private var lastFile: IFileContainer? = null
private val scaleTypeUtil = ScaleTypeUtil()
private var autoDismiss = true

// 代理监听
private val animProxyListener by lazy {
Expand All @@ -74,7 +79,10 @@ open class AnimView @JvmOverloads constructor(context: Context, attrs: Attribute
}

override fun onVideoComplete() {
hide()
if (autoDismiss)
hide()
else
lastFile?.close()
animListener?.onVideoComplete()
}

Expand All @@ -93,28 +101,30 @@ 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 = [email protected]
isOpaque = false
surfaceTextureListener = this@AnimView
layoutParams = scaleTypeUtil.getLayoutParam(this)
}
addView(innerTextureView)
}


init {
val obtainStyledAttributes = context.obtainStyledAttributes(attrs, R.styleable.AnimView)
autoDismiss = obtainStyledAttributes.getBoolean(R.styleable.AnimView_anim_view_auto_dismiss, true)
obtainStyledAttributes.recycle()
hide()
player = AnimPlayer(this)
player.autoDismiss = autoDismiss
player.animListener = animProxyListener
}


override fun prepareTextureView() {
if (onSizeChangedCalled) {
uiHandler.post(prepareTextureViewRunnable)
uiHandler.post {
removeAllViews()
innerTextureView = InnerTextureView(context).apply {
player = [email protected]
isOpaque = false
surfaceTextureListener = this@AnimView
layoutParams = scaleTypeUtil.getLayoutParam(this)
}
addView(innerTextureView)
}
} else {
ALog.e(TAG, "onSizeChanged not called")
needPrepareTextureView = true
Expand Down Expand Up @@ -188,18 +198,18 @@ open class AnimView @JvmOverloads constructor(context: Context, attrs: Attribute
}

override fun setFetchResource(fetchResource: IFetchResource?) {
player.pluginManager.getMixAnimPlugin()?.resourceRequest = fetchResource
player.pluginManager.getMixAnimPlugin().resourceRequest = fetchResource
}

override fun setOnResourceClickListener(resourceClickListener: OnResourceClickListener?) {
player.pluginManager.getMixAnimPlugin()?.resourceClickListener = resourceClickListener
player.pluginManager.getMixAnimPlugin().resourceClickListener = resourceClickListener
}

/**
* 兼容方案,优先保证表情显示
*/
open fun enableAutoTxtColorFill(enable: Boolean) {
player.pluginManager.getMixAnimPlugin()?.autoTxtColorFill = enable
fun enableAutoTxtColorFill(enable: Boolean) {
player.pluginManager.getMixAnimPlugin().autoTxtColorFill = enable
}

override fun setLoop(playLoop: Int) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
package com.tencent.qgame.animplayer

import android.media.*
import android.os.Build
import com.tencent.qgame.animplayer.file.IFileContainer
import com.tencent.qgame.animplayer.util.ALog
import com.tencent.qgame.animplayer.util.MediaUtil
import java.lang.RuntimeException

class AudioPlayer(val player: AnimPlayer) {

Expand All @@ -30,14 +30,13 @@ class AudioPlayer(val player: AnimPlayer) {
var extractor: MediaExtractor? = null
var decoder: MediaCodec? = null
var audioTrack: AudioTrack? = null
val decodeThread = HandlerHolder(null, null)
private val decodeThread = HandlerHolder(null, null)
var isRunning = false
var playLoop = 0
var isStopReq = false
var needDestroy = false



private fun prepareThread(): Boolean {
return Decoder.createThread(decodeThread, "anim_audio_thread")
}
Expand Down Expand Up @@ -97,7 +96,7 @@ class AudioPlayer(val player: AnimPlayer) {
val channelConfig = getChannelConfig(format.getInteger(MediaFormat.KEY_CHANNEL_COUNT))

val bufferSize = AudioTrack.getMinBufferSize(sampleRate, channelConfig, AudioFormat.ENCODING_PCM_16BIT)
val audioTrack = AudioTrack(AudioManager.STREAM_MUSIC, sampleRate, channelConfig, AudioFormat.ENCODING_PCM_16BIT, bufferSize, AudioTrack.MODE_STREAM)
val audioTrack = buildAudioTrack(sampleRate, channelConfig, bufferSize)
this.audioTrack = audioTrack
val state = audioTrack.state
if (state != AudioTrack.STATE_INITIALIZED) {
Expand Down Expand Up @@ -153,6 +152,31 @@ class AudioPlayer(val player: AnimPlayer) {
release()
}

@Suppress("DEPRECATION")
private fun buildAudioTrack(sampleRate: Int, channelConfig: Int, bufferSize: Int): AudioTrack {
val streamType = AudioManager.STREAM_MUSIC
val encoding = AudioFormat.ENCODING_PCM_16BIT
val modeStream = AudioTrack.MODE_STREAM
val audioTrack = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
val audioAttributes = AudioAttributes.Builder()
.setLegacyStreamType(streamType)
.build()
val audioFormat = AudioFormat.Builder()
.setSampleRate(sampleRate)
.setEncoding(encoding)
.setChannelMask(channelConfig)
.build()
AudioTrack.Builder().setAudioFormat(audioFormat)
.setAudioAttributes(audioAttributes)
.setBufferSizeInBytes(bufferSize)
.setTransferMode(modeStream)
.build()
} else {
AudioTrack(streamType, sampleRate, channelConfig, encoding, bufferSize, modeStream)
}
return audioTrack
}


private fun release() {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ abstract class Decoder(val player: AnimPlayer) : IAnimListener {
speedControlUtil.setFixedPlaybackRate(value)
field = value
}
var playLoop = 0 // 循环播放次数
var isRunning = false // 是否正在运行
var isStopReq = false // 是否需要停止
val speedControlUtil by lazy { SpeedControlUtil() }
Expand Down
Loading