tencent cloud

User Generated Short Video SDK

Android

Download
포커스 모드
폰트 크기
마지막 업데이트 시간: 2026-05-11 17:46:58

Feature Overview

Through the recording APIs, you can implement the VoiceOver feature. The TAVAudioRecorder class provides recording capabilities and supports setting the sample rate, channel count, and encoding format, including start, pause, resume, and stop methods.

Core Classes

TAVAudioRecorder

public class TAVAudioRecorder {
// Constructors
public TAVAudioRecorder(String outputPath);
public TAVAudioRecorder(String outputPath, int sampleRate, int channelCount, @PcmEncoding int pcmEncoding);
}

Error Code Definitions

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.

Notes

1. Permission Requirements:
Requires the android.permission.RECORD_AUDIO permission.
2. Thread Safety:
All method calls should be made on the same thread (a worker thread is recommended).
3. Performance Optimization:
Avoid performing time-consuming operations in processBuffer.
4. Format Support:
Output format: MP4A (AAC encoding).
Sample depth: supports 8-bit/16-bit PCM.
Channels: supports mono/stereo.

State Control Methods

Start Recording

/**
* Starts recording
* Required state: STATE_INITIALIZED or STATE_PAUSED
*/
public void start();

Pause Recording

/**
* Pauses recording
* Required state: STATE_STARTED
*/
public void pause();

Resume Recording

/**
* Resumes recording (actually calls the start method)
*/
public void resume();

Stop Recording

/**
* Stops recording and completes file writing
* Required state: any state
*/
public void stop();

Release Resources

/**
* Releases recorder resources
* Note: stop() must be called first
*/
public void release();

Parameter Configuration

Set Recording Listener

/**
* Sets the recording status callback
* @param listener recording listener
*/
public void setOnRecordingListener(OnRecordingListener listener);

Get Recorded Duration

/**
* Gets the recorded duration (microseconds)
* @return recorded duration
*/
public long getRecordedTimeUs();

Get Audio Delay

/**
* The time difference (milliseconds) from invoking audio recording to the actual start of recording
* @return audio delay value
*/
public int getDelay();

Callback Interface

OnRecordingListener

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);
}

Usage Example

Basic Recording Workflow

// 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 listener
recorder.setOnRecordingListener(new TAVAudioRecorder.OnRecordingListener() {
@Override
public byte[] processBuffer(byte[] buffer, int count) {
// Raw PCM data can be processed here
return buffer;
}

@Override
public void onFinish() {
// Handle recording completion
}

@Override
public void onError(int what) {
// Error handling
}
});

// Start recording
recorder.start();

// Pause recording
recorder.pause();

// Resume recording
recorder.resume();

// Stop recording
recorder.stop();

// Release resources
recorder.release();

Get Recording Information

// Get the recorded duration (microseconds)
long duration = recorder.getRecordedTimeUs();

// The time difference from invocation to the actual start of recording
int delay = recorder.getDelay();



도움말 및 지원

문제 해결에 도움이 되었나요?

피드백