androidandroid-mediacodecandroid-mediarecorder

MEDIA_RECORDER_INFO_MAX_FILESIZE_APPROACHING not being triggered when using MediaRecorder's setMaxFileSize


The MediaRecorder has an API that lets you automatically chunk the output stream into equally sized files.

For that you have to:

  1. Call setMaxFileSize before calling prepare with the maximum size that you want each individual file to be set to
  2. Subscribe to info notifications using OnInfoListener and call setNextOutputFile when MEDIA_RECORDER_INFO_MAX_FILESIZE_APPROACHING is reached.

The problem is that although doing everything specified by the documentation, MEDIA_RECORDER_INFO_MAX_FILESIZE_APPROACHING is never triggered and instead MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED is triggered later, after which the MediaRecorder no longer outputs data to any file.


Solution

  • The answer, unfortunately, is that Android's MediaRecorder API for setMaxFileSize not fully implemented by all the audio encoders (MPEG-4, AAC, AMR).

    If we take a close look at their C++ implementations:

    We can see that only MPEG-4's implementation notifies the MEDIA_RECORDER_INFO_MAX_FILESIZE_APPROACHING event:

    notify(MEDIA_RECORDER_EVENT_INFO, MEDIA_RECORDER_INFO_MAX_FILESIZE_APPROACHING, 0);