Skip to main content

start()

Starts a new audio recording session.
public boolean start()

Returns

  • true - Recording started successfully
  • false - Recording failed to start (usually due to MediaRecorder.prepare() throwing an IOException)

Behavior

  • Creates a new AMR file in the specified directory with a timestamp-based filename
  • Configures the MediaRecorder with AMR_NB format and MIC audio source
  • Sets the internal recording state to true

Throws

  • IllegalArgumentException - If the audio file directory is not valid or doesn’t exist

Example

AMRAudioRecorder recorder = new AMRAudioRecorder("/sdcard/recordings/");
boolean success = recorder.start();
if (success) {
    // Recording started successfully
}

pause()

Pauses the current recording session.
public boolean pause()

Returns

  • true - Recording paused successfully

Throws

  • IllegalStateException - If the recorder is not currently recording

Behavior

  • Stops the MediaRecorder and releases its resources
  • Sets the internal recording state to false
  • Preserves the current recording segment for later merging

Example

if (recorder.isRecording()) {
    recorder.pause();
}
Always check isRecording() before calling pause() to avoid IllegalStateException.

resume()

Resumes a paused recording session.
public boolean resume()

Returns

  • true - Recording resumed successfully
  • false - Recording failed to resume

Throws

  • IllegalStateException - If the recorder is already recording

Behavior

  • Creates a new recording segment that will be merged with previous segments on stop()
  • Internally calls start() to begin the new segment
  • Marks the recording as multi-file for proper merging

Example

if (!recorder.isRecording()) {
    recorder.resume();
}
When you resume after pausing, a new AMR file segment is created. These segments are automatically merged when you call stop().

stop()

Stops the recording and merges all segments into a single AMR file.
public boolean stop()

Returns

  • true - Recording stopped and files merged successfully
  • false - Failed to merge files or recorder was null

Behavior

  • If currently recording, stops the MediaRecorder and releases resources
  • Merges all recording segments into a single AMR file
  • Properly handles AMR file headers during merging (skips headers for subsequent segments)
  • Deletes individual segment files after successful merge
  • Sets the final audio path accessible via getAudioFilePath()

Example

recorder.stop();
String audioPath = recorder.getAudioFilePath();
// Use audioPath to play or upload the recording
When you pause and resume, the library creates multiple AMR file segments. The stop() method:
  1. Creates a new file with a timestamp-based filename
  2. Copies the first segment with its 6-byte AMR header
  3. Copies subsequent segments while skipping their headers
  4. Deletes the individual segment files
  5. Returns the path to the merged file
If you never paused the recording, stop() simply returns the path to the single recording file without merging.

clear()

Discards the current recording and deletes all associated files.
public void clear()

Returns

This method does not return a value (void).

Behavior

  • Stops the recorder if currently recording
  • Releases MediaRecorder resources
  • Deletes all recording segment files from the filesystem
  • Resets the internal recording state

Example

// User cancels the recording
recorder.clear();
recorder = null;
Use clear() when you want to discard a recording without saving it. This is useful for implementing a “cancel” or “delete” button in your UI.

getAudioFilePath()

Returns the path to the final merged audio file.
public String getAudioFilePath()

Returns

  • String - The absolute path to the merged AMR audio file, or null if stop() hasn’t been called yet

Example

recorder.stop();

String audioPath = recorder.getAudioFilePath();
if (audioPath != null) {
    Intent intent = new Intent(this, PlaybackActivity.class);
    intent.putExtra("audioFilePath", audioPath);
    startActivity(intent);
}
This method only returns a valid path after you call stop(). Before that, the value will be null.

isRecording()

Checks whether the recorder is currently recording.
public boolean isRecording()

Returns

  • true - The recorder is actively recording
  • false - The recorder is paused, stopped, or not yet started

Example

if (recorder.isRecording()) {
    // Show pause button
    pauseButton.setImageResource(R.drawable.ic_pause);
} else {
    // Show record/resume button
    pauseButton.setImageResource(R.drawable.ic_play);
}

Use cases

  • Updating UI state (showing play/pause buttons)
  • Validating state before calling pause() or resume()
  • Preventing invalid operations during recording

Method summary table

MethodReturn TypeDescription
start()booleanStarts a new recording session
pause()booleanPauses the current recording
resume()booleanResumes a paused recording
stop()booleanStops recording and merges segments
clear()voidDiscards recording and deletes files
getAudioFilePath()StringReturns the final audio file path
isRecording()booleanChecks if currently recording