start()
Starts a new audio recording session.Returns
true- Recording started successfullyfalse- Recording failed to start (usually due toMediaRecorder.prepare()throwing anIOException)
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
pause()
Pauses the current recording session.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
Always check
isRecording() before calling pause() to avoid IllegalStateException.resume()
Resumes a paused recording session.Returns
true- Recording resumed successfullyfalse- 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
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.Returns
true- Recording stopped and files merged successfullyfalse- 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
How file merging works
How file merging works
When you pause and resume, the library creates multiple AMR file segments. The
stop() method:- Creates a new file with a timestamp-based filename
- Copies the first segment with its 6-byte AMR header
- Copies subsequent segments while skipping their headers
- Deletes the individual segment files
- Returns the path to the merged file
stop() simply returns the path to the single recording file without merging.clear()
Discards the current recording and deletes all associated files.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
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.Returns
String- The absolute path to the merged AMR audio file, ornullifstop()hasn’t been called yet
Example
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.Returns
true- The recorder is actively recordingfalse- The recorder is paused, stopped, or not yet started
Example
Use cases
- Updating UI state (showing play/pause buttons)
- Validating state before calling
pause()orresume() - Preventing invalid operations during recording
Method summary table
| Method | Return Type | Description |
|---|---|---|
start() | boolean | Starts a new recording session |
pause() | boolean | Pauses the current recording |
resume() | boolean | Resumes a paused recording |
stop() | boolean | Stops recording and merges segments |
clear() | void | Discards recording and deletes files |
getAudioFilePath() | String | Returns the final audio file path |
isRecording() | boolean | Checks if currently recording |