diff --git a/Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/AnimConfigManager.kt b/Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/AnimConfigManager.kt index 8f12a713..5cace568 100644 --- a/Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/AnimConfigManager.kt +++ b/Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/AnimConfigManager.kt @@ -16,6 +16,7 @@ package com.tencent.qgame.animplayer import android.os.SystemClock +import com.tencent.qgame.animplayer.file.IFileContainer import com.tencent.qgame.animplayer.util.ALog import org.json.JSONObject import java.nio.charset.Charset @@ -36,7 +37,7 @@ class AnimConfigManager(val player: AnimPlayer) { * 解析配置 * @return true 解析成功 false 解析失败 */ - fun parseConfig(fileContainer: FileContainer, enableVersion1: Boolean, defaultVideoMode: Int, defaultFps: Int): Int { + fun parseConfig(fileContainer: IFileContainer, enableVersion1: Boolean, defaultVideoMode: Int, defaultFps: Int): Int { try { isParsingConfig = true // 解析vapc @@ -87,7 +88,7 @@ class AnimConfigManager(val player: AnimPlayer) { } - private fun parse(fileContainer: FileContainer, defaultVideoMode: Int, defaultFps: Int): Boolean { + private fun parse(fileContainer: IFileContainer, defaultVideoMode: Int, defaultFps: Int): Boolean { val config = AnimConfig() this.config = config diff --git a/Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/AnimPlayer.kt b/Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/AnimPlayer.kt index 66c6197c..bcc484c3 100644 --- a/Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/AnimPlayer.kt +++ b/Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/AnimPlayer.kt @@ -15,6 +15,7 @@ */ package com.tencent.qgame.animplayer +import com.tencent.qgame.animplayer.file.IFileContainer import com.tencent.qgame.animplayer.inter.IAnimListener import com.tencent.qgame.animplayer.mask.MaskConfig import com.tencent.qgame.animplayer.plugin.AnimPluginManager @@ -71,7 +72,7 @@ class AnimPlayer(val animView: IAnimView) { decoder?.onSurfaceSizeChanged(width, height) } - fun startPlay(fileContainer: FileContainer) { + fun startPlay(fileContainer: IFileContainer) { isStartRunning = true prepareDecoder() if (decoder?.prepareThread() == false) { @@ -98,7 +99,7 @@ class AnimPlayer(val animView: IAnimView) { } } - private fun innerStartPlay(fileContainer: FileContainer) { + private fun innerStartPlay(fileContainer: IFileContainer) { synchronized(AnimPlayer::class.java) { if (isSurfaceAvailable) { isStartRunning = false diff --git a/Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/AnimView.kt b/Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/AnimView.kt index 7498b133..e35a595e 100644 --- a/Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/AnimView.kt +++ b/Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/AnimView.kt @@ -22,10 +22,12 @@ import android.os.Build import android.os.Handler import android.os.Looper import android.util.AttributeSet -import android.view.MotionEvent import android.view.TextureView import android.view.View import android.widget.FrameLayout +import com.tencent.qgame.animplayer.file.AssetsFileContainer +import com.tencent.qgame.animplayer.file.FileContainer +import com.tencent.qgame.animplayer.file.IFileContainer import com.tencent.qgame.animplayer.inter.IAnimListener import com.tencent.qgame.animplayer.inter.IFetchResource import com.tencent.qgame.animplayer.inter.OnResourceClickListener @@ -51,7 +53,7 @@ open class AnimView @JvmOverloads constructor(context: Context, attrs: Attribute private var surface: SurfaceTexture? = null private var animListener: IAnimListener? = null private var innerTextureView: InnerTextureView? = null - private var lastFile: FileContainer? = null + private var lastFile: IFileContainer? = null private val scaleTypeUtil = ScaleTypeUtil() // 代理监听 @@ -235,7 +237,7 @@ open class AnimView @JvmOverloads constructor(context: Context, attrs: Attribute override fun startPlay(assetManager: AssetManager, assetsPath: String) { try { - val fileContainer = FileContainer(assetManager, assetsPath) + val fileContainer = AssetsFileContainer(assetManager, assetsPath) startPlay(fileContainer) } catch (e: Throwable) { animProxyListener.onFailed(Constant.REPORT_ERROR_TYPE_FILE_ERROR, Constant.ERROR_MSG_FILE_ERROR) @@ -243,7 +245,7 @@ open class AnimView @JvmOverloads constructor(context: Context, attrs: Attribute } - override fun startPlay(fileContainer: FileContainer) { + override fun startPlay(fileContainer: IFileContainer) { ui { if (visibility != View.VISIBLE) { ALog.e(TAG, "AnimView is GONE, can't play") diff --git a/Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/AudioPlayer.kt b/Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/AudioPlayer.kt index a82d42e9..05c211d8 100644 --- a/Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/AudioPlayer.kt +++ b/Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/AudioPlayer.kt @@ -16,6 +16,7 @@ package com.tencent.qgame.animplayer import android.media.* +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 @@ -41,7 +42,7 @@ class AudioPlayer(val player: AnimPlayer) { return Decoder.createThread(decodeThread, "anim_audio_thread") } - fun start(fileContainer: FileContainer) { + fun start(fileContainer: IFileContainer) { isStopReq = false needDestroy = false if (!prepareThread()) return @@ -63,7 +64,7 @@ class AudioPlayer(val player: AnimPlayer) { isStopReq = true } - private fun startPlay(fileContainer: FileContainer) { + private fun startPlay(fileContainer: IFileContainer) { val extractor = MediaUtil.getExtractor(fileContainer) this.extractor = extractor val audioIndex = MediaUtil.selectAudioTrack(extractor) diff --git a/Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/Decoder.kt b/Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/Decoder.kt index de9ed824..bb0843d2 100644 --- a/Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/Decoder.kt +++ b/Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/Decoder.kt @@ -18,6 +18,7 @@ package com.tencent.qgame.animplayer import android.os.Build import android.os.HandlerThread import android.os.Handler +import com.tencent.qgame.animplayer.file.IFileContainer import com.tencent.qgame.animplayer.inter.IAnimListener import com.tencent.qgame.animplayer.util.ALog import com.tencent.qgame.animplayer.util.SpeedControlUtil @@ -70,7 +71,7 @@ abstract class Decoder(val player: AnimPlayer) : IAnimListener { var isStopReq = false // 是否需要停止 val speedControlUtil by lazy { SpeedControlUtil() } - abstract fun start(fileContainer: FileContainer) + abstract fun start(fileContainer: IFileContainer) fun stop() { isStopReq = true diff --git a/Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/FileContainer.kt b/Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/FileContainer.kt deleted file mode 100644 index b4ca5f72..00000000 --- a/Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/FileContainer.kt +++ /dev/null @@ -1,98 +0,0 @@ -/* - * Tencent is pleased to support the open source community by making vap available. - * - * Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the MIT License (the "License"); you may not use this file except in - * compliance with the License. You may obtain a copy of the License at - * - * http://opensource.org/licenses/MIT - * - * Unless required by applicable law or agreed to in writing, software distributed under the License is - * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, - * either express or implied. See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.tencent.qgame.animplayer - -import android.content.res.AssetFileDescriptor -import android.content.res.AssetManager -import android.media.MediaExtractor -import java.io.File -import java.io.FileNotFoundException -import java.io.RandomAccessFile - -open class FileContainer { - - private var isAssets = false - - private var file: File? = null - private var randomAccessFile: RandomAccessFile? = null - private var assetFd: AssetFileDescriptor? = null - private var assetsInputStream: AssetManager.AssetInputStream? = null - - constructor(file: File) { - isAssets = false - this.file = file - if (!(file.exists() && file.isFile && file.canRead())) throw FileNotFoundException("Unable to read $file") - } - - constructor(assetManager: AssetManager, assetsPath: String) { - isAssets = true - assetFd = assetManager.openFd(assetsPath) - assetsInputStream = assetManager.open(assetsPath, AssetManager.ACCESS_STREAMING) as AssetManager.AssetInputStream - } - - - fun setDataSource(extractor: MediaExtractor) { - if (isAssets) { - val assetFd = this.assetFd ?: return - if (assetFd.declaredLength < 0) { - extractor.setDataSource(assetFd.fileDescriptor) - } else { - extractor.setDataSource(assetFd.fileDescriptor, assetFd.startOffset, assetFd.declaredLength) - } - } else { - val file = this.file ?: return - extractor.setDataSource(file.toString()) - } - } - - fun startRandomRead() { - if (isAssets) return - val file = this.file ?: return - randomAccessFile = RandomAccessFile(file, "r") - } - - fun read(b: ByteArray, off: Int, len: Int):Int { - return if (isAssets) { - assetsInputStream?.read(b, off, len) ?: -1 - } else { - randomAccessFile?.read(b, off, len) ?: -1 - } - } - - fun skip(pos: Long) { - if (isAssets) { - assetsInputStream?.skip(pos) - } else { - randomAccessFile?.skipBytes(pos.toInt()) - } - } - - fun closeRandomRead() { - if (isAssets) { - assetsInputStream?.close() - } else { - randomAccessFile?.close() - } - } - - fun close() { - if (isAssets) { - assetFd?.close() - assetsInputStream?.close() - } - } - -} \ No newline at end of file diff --git a/Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/HardDecoder.kt b/Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/HardDecoder.kt index d83b17fe..d4f2b719 100644 --- a/Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/HardDecoder.kt +++ b/Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/HardDecoder.kt @@ -21,6 +21,7 @@ import android.media.MediaExtractor import android.media.MediaFormat import android.os.Build import android.view.Surface +import com.tencent.qgame.animplayer.file.IFileContainer import com.tencent.qgame.animplayer.util.ALog import com.tencent.qgame.animplayer.util.MediaUtil @@ -35,7 +36,7 @@ class HardDecoder(player: AnimPlayer) : Decoder(player), SurfaceTexture.OnFrameA private val bufferInfo by lazy { MediaCodec.BufferInfo() } private var needDestroy = false - override fun start(fileContainer: FileContainer) { + override fun start(fileContainer: IFileContainer) { isStopReq = false needDestroy = false isRunning = true @@ -62,7 +63,7 @@ class HardDecoder(player: AnimPlayer) : Decoder(player), SurfaceTexture.OnFrameA } } - private fun startPlay(fileContainer: FileContainer) { + private fun startPlay(fileContainer: IFileContainer) { try { if (!prepareRender()) { throw RuntimeException("render create fail") diff --git a/Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/IAnimView.kt b/Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/IAnimView.kt index ac8ddb9c..b2d93378 100644 --- a/Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/IAnimView.kt +++ b/Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/IAnimView.kt @@ -17,6 +17,7 @@ package com.tencent.qgame.animplayer import android.content.res.AssetManager import android.graphics.SurfaceTexture +import com.tencent.qgame.animplayer.file.IFileContainer import com.tencent.qgame.animplayer.inter.IAnimListener import com.tencent.qgame.animplayer.inter.IFetchResource import com.tencent.qgame.animplayer.inter.OnResourceClickListener @@ -53,7 +54,7 @@ interface IAnimView { fun startPlay(assetManager: AssetManager, assetsPath: String) - fun startPlay(fileContainer: FileContainer) + fun startPlay(fileContainer: IFileContainer) fun stopPlay() diff --git a/Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/file/AssetsFileContainer.kt b/Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/file/AssetsFileContainer.kt new file mode 100644 index 00000000..f9a48c94 --- /dev/null +++ b/Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/file/AssetsFileContainer.kt @@ -0,0 +1,65 @@ +/* + * Tencent is pleased to support the open source community by making vap available. + * + * Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the MIT License (the "License"); you may not use this file except in + * compliance with the License. You may obtain a copy of the License at + * + * http://opensource.org/licenses/MIT + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is + * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.tencent.qgame.animplayer.file + +import android.content.res.AssetFileDescriptor +import android.content.res.AssetManager +import android.media.MediaExtractor +import com.tencent.qgame.animplayer.Constant +import com.tencent.qgame.animplayer.util.ALog + +class AssetsFileContainer(assetManager: AssetManager, assetsPath: String): IFileContainer { + + companion object { + private const val TAG = "${Constant.TAG}.FileContainer" + } + + private val assetFd: AssetFileDescriptor = assetManager.openFd(assetsPath) + private val assetsInputStream: AssetManager.AssetInputStream = + assetManager.open(assetsPath, AssetManager.ACCESS_STREAMING) as AssetManager.AssetInputStream + + init { + ALog.i(TAG, "AssetsFileContainer init") + } + + override fun setDataSource(extractor: MediaExtractor) { + if (assetFd.declaredLength < 0) { + extractor.setDataSource(assetFd.fileDescriptor) + } else { + extractor.setDataSource(assetFd.fileDescriptor, assetFd.startOffset, assetFd.declaredLength) + } + } + + override fun startRandomRead() { + } + + override fun read(b: ByteArray, off: Int, len: Int): Int { + return assetsInputStream.read(b, off, len) + } + + override fun skip(pos: Long) { + assetsInputStream.skip(pos) + } + + override fun closeRandomRead() { + assetsInputStream.close() + } + + override fun close() { + assetFd.close() + assetsInputStream.close() + } +} \ No newline at end of file diff --git a/Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/file/FileContainer.kt b/Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/file/FileContainer.kt new file mode 100644 index 00000000..e06c4f87 --- /dev/null +++ b/Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/file/FileContainer.kt @@ -0,0 +1,61 @@ +/* + * Tencent is pleased to support the open source community by making vap available. + * + * Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the MIT License (the "License"); you may not use this file except in + * compliance with the License. You may obtain a copy of the License at + * + * http://opensource.org/licenses/MIT + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is + * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.tencent.qgame.animplayer.file + +import android.media.MediaExtractor +import com.tencent.qgame.animplayer.Constant +import com.tencent.qgame.animplayer.util.ALog +import java.io.File +import java.io.FileNotFoundException +import java.io.RandomAccessFile + +class FileContainer(private val file: File) : IFileContainer { + + companion object { + private const val TAG = "${Constant.TAG}.FileContainer" + } + + private var randomAccessFile: RandomAccessFile? = null + + init { + ALog.i(TAG, "FileContainer init") + if (!(file.exists() && file.isFile && file.canRead())) throw FileNotFoundException("Unable to read $file") + } + + override fun setDataSource(extractor: MediaExtractor) { + extractor.setDataSource(file.toString()) + + } + + override fun startRandomRead() { + randomAccessFile = RandomAccessFile(file, "r") + } + + override fun read(b: ByteArray, off: Int, len: Int): Int { + return randomAccessFile?.read(b, off, len) ?: -1 + } + + override fun skip(pos: Long) { + randomAccessFile?.skipBytes(pos.toInt()) + } + + override fun closeRandomRead() { + randomAccessFile?.close() + } + + override fun close() { + } +} \ No newline at end of file diff --git a/Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/file/IFileContainer.kt b/Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/file/IFileContainer.kt new file mode 100644 index 00000000..ae4bdeee --- /dev/null +++ b/Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/file/IFileContainer.kt @@ -0,0 +1,34 @@ +/* + * Tencent is pleased to support the open source community by making vap available. + * + * Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the MIT License (the "License"); you may not use this file except in + * compliance with the License. You may obtain a copy of the License at + * + * http://opensource.org/licenses/MIT + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is + * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.tencent.qgame.animplayer.file + +import android.media.MediaExtractor + +interface IFileContainer { + + fun setDataSource(extractor: MediaExtractor) + + fun startRandomRead() + + fun read(b: ByteArray, off: Int, len: Int): Int + + fun skip(pos: Long) + + fun closeRandomRead() + + fun close() + +} \ No newline at end of file diff --git a/Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/util/MediaUtil.kt b/Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/util/MediaUtil.kt index 2964e0f8..1d4c60cb 100644 --- a/Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/util/MediaUtil.kt +++ b/Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/util/MediaUtil.kt @@ -19,7 +19,7 @@ import android.media.MediaCodecList import android.media.MediaExtractor import android.media.MediaFormat import com.tencent.qgame.animplayer.Constant -import com.tencent.qgame.animplayer.FileContainer +import com.tencent.qgame.animplayer.file.IFileContainer import kotlin.collections.HashMap @@ -35,7 +35,7 @@ object MediaUtil { } - fun getExtractor(file: FileContainer): MediaExtractor { + fun getExtractor(file: IFileContainer): MediaExtractor { val extractor = MediaExtractor() file.setDataSource(extractor) return extractor