【Android 音视频开发打怪升级:音视频硬解码篇】三、音视频播放:音视频同步(1)

  • 读取音视频数据
    */
    fun readBuffer(byteBuffer: ByteBuffer): Int

/**

  • 获取当前帧时间
    */
    fun getCurrentTimestamp(): Long

/**

  • Seek到指定位置,并返回实际帧的时间戳
    */
    fun seek(pos: Long): Long

fun setStartPos(pos: Long)

/**

  • 停止读取数据
    */
    fun stop()
    }

有了上面封装的工具,一切就变得很简单了,做一个代理转接就行了。

  • 视频提取器

class VideoExtractor(path: String): IExtractor {

private val mMediaExtractor = MMExtractor(path)

override fun getFormat(): MediaFormat? {
return mMediaExtractor.getVideoFormat()
}

override fun readBuffer(byteBuffer: ByteBuffer): Int {
return mMediaExtractor.readBuffer(byteBuffer)
}

override fun getCurrentTimestamp(): Long {
return mMediaExtractor.getCurrentTimestamp()
}

override fun seek(pos: Long): Long {
return mMediaExtractor.seek(pos)
}

override fun setStartPos(pos: Long) {
return mMediaExtractor.setStartPos(pos)
}

override fun stop() {
mMediaExtractor.stop()
}
}

  • 音频提取器

class AudioExtractor(path: String): IExtractor {

private val mMediaExtractor = MMExtractor(path)

override fun getFormat(): MediaFormat? {
return mMediaExtractor.getAudioFormat()
}

override fun readBuffer(byteBuffer: ByteBuffer): Int {
return mMediaExtractor.readBuffer(byteBuffer)
}

override fun getCurrentTimestamp(): Long {
return mMediaExtractor.getCurrentTimestamp()
}

override fun seek(pos: Long): Long {
return mMediaExtractor.seek(pos)
}

override fun setStartPos(pos: Long) {
return mMediaExtractor.setStartPos(pos)
}

override fun stop() {
mMediaExtractor.stop()
}
}

二、视频播放

我们先来定义一个视频解码器子类,继承BaseDecoder

class VideoDecoder(path: String,
sfv: SurfaceView?,
surface: Surface?): BaseDecoder(path) {
private val TAG = “VideoDecoder”

private val mSurfaceView = sfv
private var mSurface = surface

override fun check(): Boolean {
if (mSurfaceView == null && mSurface == null) {
Log.w(TAG, “SurfaceView和Surface都为空,至少需要一个不为空”)
mStateListener?.decoderError(this, “显示器为空”)
return false
}
return true
}

override fun initExtractor(path: String): IExtractor {
return VideoExtractor(path)
}

override fun initSpecParams(format: MediaFormat) {
}

override fun configCodec(codec: MediaCodec, format: MediaFormat): Boolean {
if (mSurface != null) {
codec.configure(format, mSurface , null, 0)
notifyDecode()
} else {
mSurfaceView?.holder?.addCallback(object : SurfaceHolder.Callback2 {
override fun surfaceRedrawNeeded(holder: SurfaceHolder) {
}

override fun surfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int) {
}

override fun surfaceDestroyed(holder: SurfaceHolder) {
}

override fun surfaceCreated(holder: SurfaceHolder) {
mSurface = holder.surface
configCodec(codec, fo

  • 22
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值