recording ADTS AAC Audio using AudioRecord and MediaCodec on Android but no sound when playback

708 Views Asked by At

i am trying to record audio in ADTS AAC but when recording all things going good (file is correctly create with good format an data are showing fill inside) but no sound are playing when try to open with player like VLC . can anyone please help me solve this issues ?

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;
        }
    }

}

my add ADTS headers method

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

There are 0 best solutions below