Skip to main content

Initialize the recorder

Before you can start recording, you need to create an instance of AMRAudioRecorder with a valid directory path where audio files will be stored.
String sdcardPath = Environment.getExternalStorageDirectory().getAbsolutePath();
String recordingDirectory = sdcardPath + "/wtrecorder/";

// Create the directory if it doesn't exist
File dir = new File(recordingDirectory);
if (!dir.exists()) {
    dir.mkdirs();
}

AMRAudioRecorder recorder = new AMRAudioRecorder(recordingDirectory);
The directory must exist before initializing the recorder, otherwise you’ll get an IllegalArgumentException.

Start recording

Call the start() method to begin recording audio:
boolean success = recorder.start();
if (success) {
    // Recording started successfully
} else {
    // Failed to start recording
}
The start() method:
  • Returns true if recording started successfully
  • Returns false if there was an error preparing the recorder
  • Automatically configures the MediaRecorder with AMR-NB format
  • Sets the audio source to the device microphone

Check recording status

You can check if the recorder is currently recording:
if (recorder.isRecording()) {
    // Currently recording
}

Stop recording

When you’re finished recording, call the stop() method:
boolean success = recorder.stop();
if (success) {
    // Get the final audio file path
    String audioPath = recorder.getAudioFilePath();
    // Use the audio file
}
The stop() method automatically merges multiple audio segments if you used pause/resume. See the pause and resume guide for details.

Complete example

Here’s a complete example from the sample app:
public class MainActivity extends AppCompatActivity {
    private AMRAudioRecorder recorder;
    
    public void onRecordButtonClick(View view) {
        if (recorder == null) {
            // Setup directory
            String sdcardPath = Environment.getExternalStorageDirectory().getAbsolutePath();
            String recordingDirectory = sdcardPath + "/wtrecorder/";
            File dir = new File(recordingDirectory);
            if (!dir.exists()) {
                dir.mkdirs();
            }
            
            // Start recording
            recorder = new AMRAudioRecorder(recordingDirectory);
            recorder.start();
        }
    }
    
    public void onDoneButtonClick(View view) {
        if (recorder != null) {
            recorder.stop();
            
            // Get the audio file path
            String audioPath = recorder.getAudioFilePath();
            
            recorder = null;
        }
    }
}

Audio configuration

AMRAudioRecorder uses the following configuration by default:
  • Audio source: MediaRecorder.AudioSource.MIC
  • Output format: MediaRecorder.OutputFormat.AMR_NB
  • Audio encoder: MediaRecorder.AudioEncoder.AMR_NB
  • File extension: .amr
The library generates unique filenames using timestamps to prevent file conflicts.