public class TAVAudioRecorder {// Constructorspublic TAVAudioRecorder(String outputPath);public TAVAudioRecorder(String outputPath, int sampleRate, int channelCount, @PcmEncoding int pcmEncoding);}
Error Code Constant | Value | Description |
ERROR_AUDIO_FILE_NOT_EXIST | 0x1 | Audio file does not exist. |
ERROR_AUDIO_OUT_OF_MEMORY | 0x2 | Out of memory. |
ERROR_AUDIO_ILLEGAL_ARGUMENT | 0x3 | Illegal argument. |
ERROR_AUDIO_INIT_AUDIORECORD_FAILED | 0x4 | Failed to initialize the recording device. |
ERROR_AUDIO_RECORD_START_FAILED | 0x5 | Failed to start recording. |
ERROR_AUDIO_RECORD_READ_FAILED | 0x6 | Failed to read recording data. |
android.permission.RECORD_AUDIO permission./*** Starts recording* Required state: STATE_INITIALIZED or STATE_PAUSED*/public void start();
/*** Pauses recording* Required state: STATE_STARTED*/public void pause();
/*** Resumes recording (actually calls the start method)*/public void resume();
/*** Stops recording and completes file writing* Required state: any state*/public void stop();
/*** Releases recorder resources* Note: stop() must be called first*/public void release();
/*** Sets the recording status callback* @param listener recording listener*/public void setOnRecordingListener(OnRecordingListener listener);
/*** Gets the recorded duration (microseconds)* @return recorded duration*/public long getRecordedTimeUs();
/*** The time difference (milliseconds) from invoking audio recording to the actual start of recording* @return audio delay value*/public int getDelay();
public interface OnRecordingListener {/*** Processes the recording data buffer* @param buffer raw PCM data* @param count data length* @return processed data (the original buffer can be returned directly)*/byte[] processBuffer(byte[] buffer, int count);/*** Recording completion callback*/void onFinish();/*** Error callback* @param what error code (see Error Code Definitions)*/void onError(int what);}
// Create a recording instance (output path, sample rate 44100, mono, 16-bit sampling)TAVAudioRecorder recorder = new TAVAudioRecorder("/sdcard/record.mp4a", 44100, 1, AudioFormat.ENCODING_PCM_16BIT);// Set the recording listenerrecorder.setOnRecordingListener(new TAVAudioRecorder.OnRecordingListener() {@Overridepublic byte[] processBuffer(byte[] buffer, int count) {// Raw PCM data can be processed herereturn buffer;}@Overridepublic void onFinish() {// Handle recording completion}@Overridepublic void onError(int what) {// Error handling}});// Start recordingrecorder.start();// Pause recordingrecorder.pause();// Resume recordingrecorder.resume();// Stop recordingrecorder.stop();// Release resourcesrecorder.release();
// Get the recorded duration (microseconds)long duration = recorder.getRecordedTimeUs();// The time difference from invocation to the actual start of recordingint delay = recorder.getDelay();
피드백