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 pathString sdcardPath = Environment.getExternalStorageDirectory().getAbsolutePath();String recordingDirectory = sdcardPath + "/recordings/";// Create the directory if it doesn't existFile 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");}
Always create the recording directory before initializing AMRAudioRecorder. The library throws an IllegalArgumentException if the directory doesn’t exist.
File path parameter is a directory
The constructor parameter is the directory path where audio files will be stored, not the individual file path. The library generates unique filenames automatically.
Get file path after stopping
Call getAudioFilePath() after calling stop() to retrieve the final merged audio file path.
Request permissions on Android 6.0+
On Android Marshmallow (API 23) and above, you must request RECORD_AUDIO and WRITE_EXTERNAL_STORAGE permissions at runtime before recording.