在Android上使用AudioRecord和MediaCodec录制ADTS AAC音频,但播放时无声音

时间:2018-10-03 15:56:04

标签: android mediacodec aac adts

我正在尝试用ADTS AAC录制音频,但是当录制一切正常时(正确创建了格式正确的文件,数据正在填充内部),但是尝试像VLC这样的播放器打开时没有声音在播放。谁能帮我解决这个问题?

enter code here private void getDataFromEncoder(byte[] data, int size){

    ByteBuffer[] codecInputBuffers = codec.getInputBuffers();
    ByteBuffer[] codecOutputBuffers = codec.getOutputBuffers();
    long pts = System.nanoTime() / 1000 - mPresentTimeUs;

    int inBufferIndex = codec.dequeueInputBuffer( -1 ); // kTimeoutUs ou -1
    if (inBufferIndex >= 0) {
        ByteBuffer buffer = codecInputBuffers[inBufferIndex];
        buffer.clear();
       // size = buffer.limit();
        byte[] zeroes = new byte[size];
        buffer.put(zeroes);
        // buffer.put(data, 0, size);
        codec.queueInputBuffer(inBufferIndex, 0 , size,  pts , 0);

    }

    for (; ; ) {
        int outBufferIndex = codec.dequeueOutputBuffer(audioInfo, 0 ); // kTimeoutUs
        // outBufferIndex vaut -2

        if (outBufferIndex == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED) { //si le format a changer
            onAudioFormat(codec.getOutputFormat()); // on recupere la valeur du nouveau format mediaformat
              /*mediaformat = codec.getOutputFormat();*/

        } else if (outBufferIndex >= 0) {
            //This ByteBuffer is AAC
            int outBitsSize = audioInfo.size; //
            int outPacketSize = outBitsSize + 7;    // 7 is ADTS size
            ByteBuffer outBuf = codecOutputBuffers[outBufferIndex];

            outBuf.position(audioInfo.offset);
            outBuf.limit(audioInfo.offset + outBitsSize);
            try {
               // data = new byte[outPacketSize];  //space for ADTS header included
                addADTStoPacket(data, outPacketSize);
                outBuf.get(data, 7, outBitsSize);
                outBuf.position(audioInfo.offset);
                mFileStream.write(data, 0, outPacketSize);
            } catch (IOException e) {
                Log.e(TAG, "failed writing bitstream data to file");
                e.printStackTrace();
            }


            outBuf.clear();
            codec.releaseOutputBuffer(outBufferIndex, false);

        } else {
            break;
        }
    }

}

我添加ADTS标头方法

enter code here     private void addADTStoPacket(byte[] packet, int packetLen) {
    int profile = 2;  //AAC LC
    //39=MediaCodecInfo.CodecProfileLevel.AACObjectELD;
    int freqIdx = 4;  //44.1KHz
    int chanCfg = 2;  //CPE

    // fill in ADTS data
    packet[0] = (byte)0xFF; // conversion hexadecimal a decimal - il y a seize unités de 0 à F, on parle donc d'hexadécimal.
    packet[1] = (byte)0xF9; // installe la version ADTS dans MPEG-2 (0xF1) au lieu de MPEG-4 (0xF9)
    packet[2] = (byte)(((profile-1)<<6) + (freqIdx<<2) +(chanCfg>>2));
    packet[3] = (byte)(((chanCfg&3)<<6) + (packetLen>>11));
    packet[4] = (byte)((packetLen&0x7FF) >> 3);
    packet[5] = (byte)(((packetLen&7)<<5) + 0x1F);
    packet[6] = (byte)0xFC; // 0xFC est également correct si vous ne connaissez pas la valeur de la plénitude du tampon
}

0 个答案:

没有答案