Skip to main content
This guide walks you through creating a basic audio recording app using AMRAudioRecorder.

Prerequisites

Before you begin, make sure you’ve completed the installation steps.

Basic recording example

Here’s a complete example showing how to implement recording with pause/resume functionality:
1

Set up the recording directory

Create a directory where audio files will be stored:
import android.os.Environment;
import java.io.File;

// Get the external storage path
String sdcardPath = Environment.getExternalStorageDirectory().getAbsolutePath();
String recordingDirectory = sdcardPath + "/recordings/";

// Create the directory if it doesn't exist
File dir = new File(recordingDirectory);
if (!dir.exists()) {
    dir.mkdirs();
}
The directory must exist before creating the recorder instance. AMRAudioRecorder will not create the directory for you.
2

Initialize the recorder

Create an instance of AMRAudioRecorder with your directory path:
import com.water.amraudiorecorder.AMRAudioRecorder;

AMRAudioRecorder recorder = new AMRAudioRecorder(recordingDirectory);
3

Start recording

Begin recording audio:
boolean started = recorder.start();
if (started) {
    // Recording started successfully
    Log.d("Recorder", "Recording started");
} else {
    // Failed to start recording
    Log.e("Recorder", "Failed to start recording");
}
4

Pause and resume

Pause the recording and resume when needed:
// Pause recording
recorder.pause();
Log.d("Recorder", "Recording paused");

// Resume recording
recorder.resume();
Log.d("Recorder", "Recording resumed");
5

Stop and get the file

Stop recording and retrieve the final audio file path:
boolean stopped = recorder.stop();
if (stopped) {
    String audioFilePath = recorder.getAudioFilePath();
    Log.d("Recorder", "Recording saved to: " + audioFilePath);
    
    // Use the audio file (play it, upload it, etc.)
}

Complete activity example

Here’s a complete MainActivity implementation with a recording button:
MainActivity.java
package com.yourapp.example;

import android.Manifest;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import com.water.amraudiorecorder.AMRAudioRecorder;

import java.io.File;

public class MainActivity extends AppCompatActivity {
    
    private AMRAudioRecorder recorder;
    private Button recordButton;
    private Button stopButton;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        recordButton = findViewById(R.id.recordButton);
        stopButton = findViewById(R.id.stopButton);
        
        // Request permissions (Android 6.0+)
        ActivityCompat.requestPermissions(this,
            new String[]{Manifest.permission.RECORD_AUDIO,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE},
            1);
        
        recordButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toggleRecording();
            }
        });
        
        stopButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                stopRecording();
            }
        });
    }
    
    private void toggleRecording() {
        if (recorder == null) {
            startRecording();
        } else {
            if (recorder.isRecording()) {
                recorder.pause();
                recordButton.setText("Resume");
                Toast.makeText(this, "Recording paused", Toast.LENGTH_SHORT).show();
            } else {
                recorder.resume();
                recordButton.setText("Pause");
                Toast.makeText(this, "Recording resumed", Toast.LENGTH_SHORT).show();
            }
        }
    }
    
    private void startRecording() {
        // Set up directory
        String sdcardPath = Environment.getExternalStorageDirectory().getAbsolutePath();
        String recordingDirectory = sdcardPath + "/recordings/";
        File dir = new File(recordingDirectory);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        
        // Create and start recorder
        recorder = new AMRAudioRecorder(recordingDirectory);
        boolean started = recorder.start();
        
        if (started) {
            recordButton.setText("Pause");
            stopButton.setEnabled(true);
            Toast.makeText(this, "Recording started", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "Failed to start recording", Toast.LENGTH_SHORT).show();
        }
    }
    
    private void stopRecording() {
        if (recorder == null) {
            return;
        }
        
        boolean stopped = recorder.stop();
        if (stopped) {
            String filePath = recorder.getAudioFilePath();
            Toast.makeText(this, "Recording saved: " + filePath, Toast.LENGTH_LONG).show();
        }
        
        // Reset UI
        recorder = null;
        recordButton.setText("Start Recording");
        stopButton.setEnabled(false);
    }
}

Checking recording status

You can check if the recorder is currently recording:
if (recorder.isRecording()) {
    // Currently recording
} else {
    // Paused or not started
}

Clearing recordings

If you want to discard a recording without saving it:
recorder.clear();
Calling clear() will delete all temporary audio files and stop the recording. This action cannot be undone.

Error handling

Handle common errors when working with AMRAudioRecorder:
try {
    // Directory doesn't exist
    AMRAudioRecorder recorder = new AMRAudioRecorder("/invalid/path/");
} catch (IllegalArgumentException e) {
    // Handle invalid directory error
    Log.e("Recorder", "Invalid directory: " + e.getMessage());
}

try {
    // Trying to pause when not recording
    recorder.pause();
} catch (IllegalStateException e) {
    // Handle state error
    Log.e("Recorder", "Invalid state: " + e.getMessage());
}

Key points to remember

Always create the recording directory before initializing AMRAudioRecorder. The library throws an IllegalArgumentException if the directory doesn’t exist.
The constructor parameter is the directory path where audio files will be stored, not the individual file path. The library generates unique filenames automatically.
Call getAudioFilePath() after calling stop() to retrieve the final merged audio file path.
On Android Marshmallow (API 23) and above, you must request RECORD_AUDIO and WRITE_EXTERNAL_STORAGE permissions at runtime before recording.

Next steps

API reference

Explore all available methods and their parameters

Guides

Learn advanced techniques and best practices

Build docs developers (and LLMs) love