使用AudioRecorder录制aac格式的音频,要有暂停功能
时间: 2024-02-27 11:57:45 浏览: 132
要使用AudioRecorder录制aac格式的音频并实现暂停功能,需要使用MediaCodec进行编码。以下是一个简单的实现示例:
```java
public class AacRecorder {
private static final String TAG = AacRecorder.class.getSimpleName();
private static final int SAMPLE_RATE = 44100;
private static final int BIT_RATE = 128000;
private static final int CHANNEL_COUNT = 1;
private static final int TIMEOUT_US = 10000;
private static final int MAX_BUFFER_SIZE = 16384;
private static final String MIME_TYPE = "audio/mp4a-latm";
private AudioRecord mAudioRecord;
private MediaCodec mMediaCodec;
private String mOutputFile;
private FileOutputStream mFileOutputStream;
private boolean mIsRecording = false;
private boolean mIsPaused = false;
private long mPresentationTimeUs = 0;
public AacRecorder(String outputFile) {
mOutputFile = outputFile;
}
public void start() {
try {
mAudioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, SAMPLE_RATE, CHANNEL_COUNT, AudioFormat.ENCODING_PCM_16BIT, AudioRecord.getMinBufferSize(SAMPLE_RATE, CHANNEL_COUNT, AudioFormat.ENCODING_PCM_16BIT));
MediaFormat format = MediaFormat.createAudioFormat(MIME_TYPE, SAMPLE_RATE, CHANNEL_COUNT);
format.setInteger(MediaFormat.KEY_BIT_RATE, BIT_RATE);
format.setInteger(MediaFormat.KEY_AAC_PROFILE, MediaCodecInfo.CodecProfileLevel.AACObjectLC);
mMediaCodec = MediaCodec.createEncoderByType(MIME_TYPE);
mMediaCodec.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
mMediaCodec.start();
mFileOutputStream = new FileOutputStream(mOutputFile);
} catch (IOException | IllegalStateException e) {
e.printStackTrace();
return;
}
mIsRecording = true;
mIsPaused = false;
new Thread(new Runnable() {
@Override
public void run() {
try {
ByteBuffer[] inputBuffers = mMediaCodec.getInputBuffers();
ByteBuffer[] outputBuffers = mMediaCodec.getOutputBuffers();
MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo();
while (mIsRecording) {
if (!mIsPaused) {
int inputBufferIndex = mMediaCodec.dequeueInputBuffer(TIMEOUT_US);
if (inputBufferIndex >= 0) {
ByteBuffer inputBuffer = inputBuffers[inputBufferIndex];
inputBuffer.clear();
int bytesRead = mAudioRecord.read(inputBuffer, MAX_BUFFER_SIZE);
if (bytesRead > 0) {
mMediaCodec.queueInputBuffer(inputBufferIndex, 0, bytesRead, mPresentationTimeUs, 0);
mPresentationTimeUs += (long) (1.0f / SAMPLE_RATE * 1000000);
}
}
int outputBufferIndex = mMediaCodec.dequeueOutputBuffer(bufferInfo, TIMEOUT_US);
while (outputBufferIndex >= 0) {
ByteBuffer outputBuffer = outputBuffers[outputBufferIndex];
outputBuffer.position(bufferInfo.offset);
outputBuffer.limit(bufferInfo.offset + bufferInfo.size);
byte[] data = new byte[bufferInfo.size];
outputBuffer.get(data);
mFileOutputStream.write(data);
Log.d(TAG, "Recorded " + data.length + " bytes");
mMediaCodec.releaseOutputBuffer(outputBufferIndex, false);
outputBufferIndex = mMediaCodec.dequeueOutputBuffer(bufferInfo, TIMEOUT_US);
}
}
}
mAudioRecord.stop();
mAudioRecord.release();
mMediaCodec.stop();
mMediaCodec.release();
mFileOutputStream.flush();
mFileOutputStream.close();
} catch (IOException | IllegalStateException e) {
e.printStackTrace();
}
}
}).start();
mAudioRecord.startRecording();
}
public void pause() {
mIsPaused = true;
}
public void resume() {
mIsPaused = false;
}
public void stop() {
mIsRecording = false;
}
public boolean isRecording() {
return mIsRecording;
}
public boolean isPaused() {
return mIsPaused;
}
}
```
在这个示例中,我们创建了一个AacRecorder类来封装录制AAC音频的功能。在start()方法中,我们创建了一个AudioRecord实例来录制音频,并创建了一个MediaCodec实例来进行AAC编码。在录音任务中,我们使用一个while循环来读取录音数据,并将其传递给MediaCodec进行编码。在编码完成后,我们将编码后的数据写入文件中。
在pause()和resume()方法中,我们分别设置mIsPaused标志为true和false,以控制录音任务的暂停和继续。
在stop()方法中,我们将mIsRecording标志设置为false,以停止录音任务,并释放AudioRecord和MediaCodec资源。
请注意,这只是一个简单的示例,实际应用中可能需要更复杂的逻辑来处理录音暂停和继续的情况。
阅读全文