androidaudiowavamr

Android - get duration of AMR audio file programmatically


How do I get duration of an AMR file?

mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

I want to get the duration of the file after the recording is stopped WITHOUT creating any MediaPlayer and get the duration from it. For a regular Wav file I simply do this:

fileLength / byteRate

but for AMR I didn't know the byteRate and I'm not sure this will be ok though since WAV is raw PCM data(uncompressed) and AMR is compressed.


Solution

  • Maybe the 3GP container contains information about the content length? The 3GPP file format spec is available if you want to read it.


    For a raw .amr file you'd have to traverse all the frames to find the length of the audio, since each frame can be encoded with a different bitrate.

    The process for doing this would be:

    If you've counted the number of frames in the file you can multiply that number by 20 to get the length of the audio in milliseconds.

    Frame size table:

    Codec mode  Frame size
    ----------------------
    0             13
    1             14
    2             16
    3             18
    4             20
    5             21
    6             27
    7             32
    

    (Source)