1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
|
fun createMediaCodecDecoder(mime: String, sampleRate:Int){
val format = MediaFormat.createAudioFormat(mime,sampleRate,1) format.setInteger(MediaFormat.KEY_CHANNEL_MASK, AudioFormat.CHANNEL_IN_MONO); format.setInteger(MediaFormat.KEY_BIT_RATE, 128000)
val mMediaCodec = MediaCodec.createDecoderByType(mime) mMediaCodec.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE)
mMediaCodec.setCallback(object :MediaCodec.Callback(){
override fun onInputBufferAvailable(codec: MediaCodec, index: Int) {
val dataLength = 2 val data = ByteArray(dataLength) val inputBuffer = codec.getInputBuffer(index) if(inputBuffer != null){ inputBuffer.clear() inputBuffer.put(data) codec.queueInputBuffer(index,0,dataLength,0,0) }else{ codec.queueInputBuffer(index,0,0,0,0) } }
override fun onOutputBufferAvailable( codec: MediaCodec, index: Int, info: MediaCodec.BufferInfo ) { try { if(info.size > 0){
if ((info.flags and MediaCodec.BUFFER_FLAG_CODEC_CONFIG) != 0) { println("解码器输出配置信息") }else{ println("解码器输出编码数据") }
val outputBuffer = codec.getOutputBuffer(index) val outputData = ByteArray(info.size) outputBuffer?.get(outputData) } }catch (e:Exception){ e.printStackTrace() }finally { codec.releaseOutputBuffer(index, false) } }
override fun onError(codec: MediaCodec, e: MediaCodec.CodecException) { e.printStackTrace() }
override fun onOutputFormatChanged(codec: MediaCodec, format: MediaFormat) { println("onOutputFormatChanged") } }) mMediaCodec.start() }
|