Android使用MediaCodec编码录音,之后使用SmartPublisherPostAudioEncodedData(long handle, int codec_id, ByteBuffer data, int size, int is_key_frame, long timestamp,ByteBuffer parameter_info, int parameter_info_size)直播推流的完整代码
时间: 2024-03-10 07:44:34 浏览: 54
以下是一个使用 MediaCodec 编码录音,并使用 SmartPublisher 推流的示例代码:
```
public class AudioEncoder {
// 音频相关参数
private static final String MIME_TYPE = "audio/mp4a-latm";
private static final int SAMPLE_RATE = 44100;
private static final int CHANNEL_COUNT = 1;
private static final int BIT_RATE = 64000;
// 编码器相关参数
private MediaCodec mMediaCodec;
private ByteBuffer[] mInputBuffers;
private ByteBuffer[] mOutputBuffers;
private MediaCodec.BufferInfo mBufferInfo;
// 推流相关参数
private SmartPublisherJniV2 mPublisher;
private long mPublisherHandle;
public AudioEncoder() {
// 初始化编码器
initMediaCodec();
// 初始化推流器
initPublisher();
}
private void initMediaCodec() {
try {
// 创建 MediaCodec
mMediaCodec = MediaCodec.createEncoderByType(MIME_TYPE);
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.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
mMediaCodec.start();
// 获取输入输出缓冲区
mInputBuffers = mMediaCodec.getInputBuffers();
mOutputBuffers = mMediaCodec.getOutputBuffers();
mBufferInfo = new MediaCodec.BufferInfo();
} catch (IOException e) {
e.printStackTrace();
}
}
private void initPublisher() {
mPublisher = new SmartPublisherJniV2();
mPublisherHandle = mPublisher.SmartPublisherOpen("rtmp://xxx.xxx.xxx.xxx/appname/streamname", 0, 0);
mPublisher.SmartPublisherSetAudioCodecType(mPublisherHandle, 1);
}
public void start() {
// 开始录音
AudioRecord audioRecord = new AudioRecord(MediaRecorder.AudioSource.DEFAULT, SAMPLE_RATE, CHANNEL_COUNT, AudioFormat.ENCODING_PCM_16BIT, AudioRecord.getMinBufferSize(SAMPLE_RATE, CHANNEL_COUNT, AudioFormat.ENCODING_PCM_16BIT));
audioRecord.startRecording();
// 不断读取录音数据并编码、推流
while (true) {
int inputBufferIndex = mMediaCodec.dequeueInputBuffer(-1);
if (inputBufferIndex >= 0) {
ByteBuffer inputBuffer = mInputBuffers[inputBufferIndex];
inputBuffer.clear();
// 读取录音数据
byte[] audioData = new byte[inputBuffer.capacity()];
int readSize = audioRecord.read(audioData, 0, audioData.length);
if (readSize > 0) {
inputBuffer.put(audioData);
mMediaCodec.queueInputBuffer(inputBufferIndex, 0, readSize, System.nanoTime() / 1000, 0);
}
}
int outputBufferIndex = mMediaCodec.dequeueOutputBuffer(mBufferInfo, 0);
while (outputBufferIndex >= 0) {
ByteBuffer outputBuffer = mOutputBuffers[outputBufferIndex];
// 发送编码后的音频数据
byte[] data = new byte[mBufferInfo.size];
outputBuffer.get(data);
mPublisher.SmartPublisherPostAudioEncodedData(mPublisherHandle, 1, ByteBuffer.wrap(data), data.length, mBufferInfo.flags == MediaCodec.BUFFER_FLAG_KEY_FRAME, mBufferInfo.presentationTimeUs / 1000, null, 0);
mMediaCodec.releaseOutputBuffer(outputBufferIndex, false);
outputBufferIndex = mMediaCodec.dequeueOutputBuffer(mBufferInfo, 0);
}
}
}
public void stop() {
mMediaCodec.stop();
mMediaCodec.release();
mPublisher.SmartPublisherClose(mPublisherHandle);
}
}
```
注意:以上代码仅为示例代码,仅供参考。实际使用时需根据具体情况进行修改和完善。
阅读全文