|
| 1 | +package com.zfg.audiodemo; |
| 2 | + |
| 3 | +import android.media.MediaCodec; |
| 4 | +import android.media.MediaExtractor; |
| 5 | +import android.media.MediaFormat; |
| 6 | +import android.os.Environment; |
| 7 | +import android.util.Log; |
| 8 | + |
| 9 | +import java.io.File; |
| 10 | +import java.io.FileNotFoundException; |
| 11 | +import java.io.FileOutputStream; |
| 12 | +import java.io.IOException; |
| 13 | +import java.nio.ByteBuffer; |
| 14 | + |
| 15 | +import static com.zfg.audiodemo.AudioEncoder.ENCODER_FILE; |
| 16 | +import static com.zfg.audiodemo.MainActivity.FOLDER_NAME; |
| 17 | +import static com.zfg.audiodemo.MainActivity.TAG; |
| 18 | + |
| 19 | +/** |
| 20 | + * 音频硬解码 |
| 21 | + */ |
| 22 | +public class AudioDecoder { |
| 23 | + |
| 24 | + //用于分离出音频轨道 |
| 25 | + private MediaExtractor mMediaExtractor; |
| 26 | + private MediaCodec mMediaCodec; |
| 27 | + private MediaCodec.BufferInfo mBufferInfo; |
| 28 | + private File mTargetFile; |
| 29 | + private File mPCMFile; |
| 30 | + private FileOutputStream mFileOutputStream; |
| 31 | + private IDecodeDelegate mIDecodeDelegate; |
| 32 | + |
| 33 | + /** |
| 34 | + * AAC格式 |
| 35 | + */ |
| 36 | + private String MINE_TYPE_AAC = "audio/mp4a-latm"; |
| 37 | + |
| 38 | + /** |
| 39 | + * 解码后的pcm文件 |
| 40 | + */ |
| 41 | + public static final String DECODER_FILE = "decoder_file"; |
| 42 | + |
| 43 | + /** |
| 44 | + * 初始化解码器 |
| 45 | + */ |
| 46 | + public void initDecoder(IDecodeDelegate iDecodeDelegate) { |
| 47 | + |
| 48 | + mIDecodeDelegate = iDecodeDelegate; |
| 49 | + |
| 50 | + File fileFolder = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + FOLDER_NAME); |
| 51 | + String fileFolderPath = fileFolder.getAbsolutePath(); |
| 52 | + mTargetFile = new File(fileFolderPath + "/" + ENCODER_FILE + ".aac"); |
| 53 | + if (!mTargetFile.exists()) { |
| 54 | + Log.e(TAG, "The source file does not exist!"); |
| 55 | + return; |
| 56 | + } |
| 57 | + mPCMFile = new File(fileFolderPath + "/" + DECODER_FILE + ".pcm"); |
| 58 | + if (!mPCMFile.exists()) { |
| 59 | + try { |
| 60 | + mPCMFile.createNewFile(); |
| 61 | + } catch (IOException e) { |
| 62 | + e.printStackTrace(); |
| 63 | + } |
| 64 | + } |
| 65 | + try { |
| 66 | + mFileOutputStream = new FileOutputStream(mPCMFile.getAbsoluteFile()); |
| 67 | + } catch (FileNotFoundException e) { |
| 68 | + e.printStackTrace(); |
| 69 | + } |
| 70 | + mMediaExtractor = new MediaExtractor(); |
| 71 | + try { |
| 72 | + //设置资源 |
| 73 | + mMediaExtractor.setDataSource(mTargetFile.getAbsolutePath()); |
| 74 | + //获取含有音频的MediaFormat |
| 75 | + MediaFormat mediaFormat = createMediaFormat(); |
| 76 | + mMediaCodec = MediaCodec.createDecoderByType(MINE_TYPE_AAC); |
| 77 | + //解码时最后一个参数设置为0 |
| 78 | + mMediaCodec.configure(mediaFormat, null, null, 0); |
| 79 | + if (mMediaCodec == null) { |
| 80 | + Log.e(TAG, "Create media decode failed"); |
| 81 | + return; |
| 82 | + } |
| 83 | + //进入Runnable状态 |
| 84 | + mMediaCodec.start(); |
| 85 | + mBufferInfo = new MediaCodec.BufferInfo(); |
| 86 | + Log.i(TAG, "Create media decode succeed"); |
| 87 | + } catch (IOException e) { |
| 88 | + e.printStackTrace(); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + private MediaFormat createMediaFormat() { |
| 93 | + //获取文件的轨道数,做循环得到含有音频的mediaFormat |
| 94 | + for (int i = 0; i < mMediaExtractor.getTrackCount(); i++) { |
| 95 | + MediaFormat mediaFormat = mMediaExtractor.getTrackFormat(i); |
| 96 | + //MediaFormat键值对应 |
| 97 | + String mime = mediaFormat.getString(MediaFormat.KEY_MIME); |
| 98 | + if (mime.contains("audio/")) { |
| 99 | + mMediaExtractor.selectTrack(i); |
| 100 | + return mediaFormat; |
| 101 | + } |
| 102 | + } |
| 103 | + return null; |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * 开始解码 |
| 108 | + */ |
| 109 | + public void decode() { |
| 110 | + |
| 111 | + boolean inputSawEos = false; |
| 112 | + boolean outputSawEos = false; |
| 113 | + long timeout = 5000; |
| 114 | + |
| 115 | + while (!outputSawEos) { |
| 116 | + if (!inputSawEos) { |
| 117 | + //每5000毫秒查询一次 |
| 118 | + int inputIndex = mMediaCodec.dequeueInputBuffer(timeout); |
| 119 | + //输入缓存index可用 |
| 120 | + if (inputIndex >= 0) { |
| 121 | + //获取可用的输入缓存 |
| 122 | + ByteBuffer inputBuffer = mMediaCodec.getInputBuffer(inputIndex); |
| 123 | + //从MediaExtractor读取数据到输入缓存中,返回读取长度 |
| 124 | + int bufferSize = mMediaExtractor.readSampleData(inputBuffer, 0); |
| 125 | + if (bufferSize <= 0) {//已经读取完 |
| 126 | + //标志输入完毕 |
| 127 | + inputSawEos = true; |
| 128 | + //做标识 |
| 129 | + mMediaCodec.queueInputBuffer(inputIndex, 0, 0, timeout, MediaCodec.BUFFER_FLAG_END_OF_STREAM); |
| 130 | + } else { |
| 131 | + long time = mMediaExtractor.getSampleTime(); |
| 132 | + //将输入缓存放入MediaCodec中 |
| 133 | + mMediaCodec.queueInputBuffer(inputIndex, 0, bufferSize, time, 0); |
| 134 | + //指向下一帧 |
| 135 | + mMediaExtractor.advance(); |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + //获取输出缓存,需要传入MediaCodec.BufferInfo 用于存储ByteBuffer信息 |
| 140 | + int outputIndex = mMediaCodec.dequeueOutputBuffer(mBufferInfo, timeout); |
| 141 | + if (outputIndex >= 0) { |
| 142 | + if ((mBufferInfo.flags & MediaCodec.BUFFER_FLAG_CODEC_CONFIG) != 0) { |
| 143 | + mMediaCodec.releaseOutputBuffer(outputIndex, false); |
| 144 | + continue; |
| 145 | + } |
| 146 | + //有输出数据 |
| 147 | + if (mBufferInfo.size > 0) { |
| 148 | + //获取输出缓存 |
| 149 | + ByteBuffer outputBuffer = mMediaCodec.getOutputBuffer(outputIndex); |
| 150 | + //设置ByteBuffer的position位置 |
| 151 | + outputBuffer.position(mBufferInfo.offset); |
| 152 | + //设置ByteBuffer访问的结点 |
| 153 | + outputBuffer.limit(mBufferInfo.offset + mBufferInfo.size); |
| 154 | + byte[] targetData = new byte[mBufferInfo.size]; |
| 155 | + //将数据填充到数组中 |
| 156 | + outputBuffer.get(targetData); |
| 157 | + try { |
| 158 | + mFileOutputStream.write(targetData); |
| 159 | + } catch (IOException e) { |
| 160 | + e.printStackTrace(); |
| 161 | + } |
| 162 | + } |
| 163 | + //释放输出缓存 |
| 164 | + mMediaCodec.releaseOutputBuffer(outputIndex, false); |
| 165 | + //判断缓存是否完结 |
| 166 | + if ((mBufferInfo.flags & MediaCodec.BUFFER_FLAG_END_OF_STREAM) != 0) { |
| 167 | + outputSawEos = true; |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | + //释放资源 |
| 172 | + try { |
| 173 | + mFileOutputStream.flush(); |
| 174 | + mFileOutputStream.close(); |
| 175 | + mMediaCodec.stop(); |
| 176 | + mMediaCodec.release(); |
| 177 | + mMediaExtractor.release(); |
| 178 | + if (null != mIDecodeDelegate) { |
| 179 | + mIDecodeDelegate.decodeResult(true); |
| 180 | + } |
| 181 | + } catch (IOException e) { |
| 182 | + e.printStackTrace(); |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + /** |
| 187 | + * 是否解码完成回调 |
| 188 | + */ |
| 189 | + interface IDecodeDelegate { |
| 190 | + void decodeResult(boolean isFinish); |
| 191 | + } |
| 192 | +} |
0 commit comments