'write COMMENT tag to WAV and M4A

I have an audio app and I need to read and write the COMMENT tag of WAV and M4A files. This is all I need. I had struggled for hours without success.

The closest I arrived is using com.github.goxr3plus:jaudiotagger:2.2.7. I had success with WAV written with AudioRecord but exception with M4A written with MediaRecorder.

My question is: How to write the COMMENT tag on files with AudioRecord? I know how to write a WAV file using AudioRecord, including its headers, but I had failed to find a CLEAR specification, like this one for the headers.

And yet, in any case, I do not know how to add the COMMENT header for M4A files, with or without external libraries. Clues?



Solution 1:[1]

After surfing the web a lot I found the solution here, where the crucial information is "where '....' represents 4 bytes that show the length of each 'chunk'". So I did the following method that builds the ICMT chunk for me:

byte[] getCMT(String cmt) {
    byte[] cmtBytes = cmt.getBytes();
    int l = cmtBytes.length+12;
    byte[] result = new byte[l+8];
    byte[] list = new byte[]{'L', 'I', 'S', 'T',
            (byte) (l & 0xff),(byte) ((l >> 8) & 0xff),(byte) ((l >> 16) & 0xff),(byte) ((l >> 24) & 0xff)};
    l=l-12;
    byte[] info = new byte[]{'I','N','F','O','I','C','M','T',
            (byte) (l & 0xff),(byte) ((l >> 8) & 0xff),(byte) ((l >> 16) & 0xff),(byte) ((l >> 24) & 0xff)};

    System.arraycopy(list, 0, result, 0, list.length);
    System.arraycopy(info, 0, result, list.length, info.length);
    System.arraycopy(cmtBytes, 0, result, list.length+info.length, cmtBytes.length);
    return result;
}

Now it is easy:

for (byte datum : getCMT(MyTextComment))
      outStream.write(datum);

The only thing to be careful is to choose the string MyTextComment with even length.

Sorry if this is well known, adding it for reference since it took me waaaaay too much time.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Luis A. Florit