I am trying to combine 2 AAC files into one, I found out that in AAC files - the header element is present till the FF8 byte, and then next 4 bytes contain the length of the data of the AAC. I tried to maintain 1 header array , add the size of the 2 AAC files, and then add the data buffer of the two files one after the other.

The resultant file played only the first AAC file. Here is the code snippet.

FileInputStream fs = new FileInputStream("./res/after.aac");

dis = new DataInputStream(fs);
headerData = new byte[0xFF8];
dis.read(headerData);


int lengthTotal = dis.readInt();
System.out.println("Length of After == "+lengthTotal);
dis.readInt();


data = new byte[dis.available()];

dis.readFully(data);
dis.close();
dis = null;
fs.close();
fs = null;


fs = new FileInputStream("./res/continue.aac");
dis = new DataInputStream(fs);

dis.skipBytes(0xFF8);

int length = dis.readInt();
System.out.println("Length of Ahead == "+length);
lengthTotal = lengthTotal + length -8;
System.out.println("Total Length== "+lengthTotal);
dis.readInt();
newData = new byte[dis.available()];
dis.read(newData);

FileOutputStream fos = new FileOutputStream("./res/combine.aac");
DataOutputStream dos = new DataOutputStream(fos);

dos.write(headerData);
dos.writeInt(lengthTotal);
dos.writeBytes("mdat");
dos.write(data);
dos.write(newData);

I know that there is information about time duration of the AAC file in the 56th byte, but I am not being able to figure it out. Can someone help me out here?

Edit
Report