我正在尝试将音频添加到由以下开源项目创建的视频中
具体到https://github.com/madisp/trails/blob/master/app/src/main/java/com/madisp/trails/CaptureService.java
我需要从 MIC 获取音频并将其作为音轨写入编码文件。目前用 Muxer 编码的文件只有视频轨道。
我可以从 MIC 获取音频,没有任何问题
int nChannels = 1;
int minBufferSize = AudioRecord.getMinBufferSize(44100, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT) * 2;
AudioRecord aRecorder = new AudioRecord(MediaRecorder.AudioSource.MIC, 44100, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, minBufferSize);
short[] buffer = new short[44100 * nChannels];
aRecorder.startRecording();
int readSize = 0;
while (recording) {
readSize = aRecorder.read(buffer, 0, minBufferSize);
if (readSize < 0) {
break;
} else if (readSize > 0) {
// do stuff with buffer
}
}
aRecorder.stop();
aRecorder.release();
但我不知道如何将其合并到(https://github.com/madisp/trails/blob/master/app/src/main/java/com/madisp/trails/CaptureService.java)
while (running) {
int index = avc.dequeueOutputBuffer(info, 10000);
if (index == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED) {
if (track != -1) {
throw new RuntimeException("format changed twice");
}
track = muxer.addTrack(avc.getOutputFormat());
muxer.start();
} else if (index >= 0) {
if ((info.flags & MediaCodec.BUFFER_FLAG_CODEC_CONFIG) != 0) {
// ignore codec config
info.size = 0;
}
if (track != -1) {
ByteBuffer out = avc.getOutputBuffer(index);
out.position(info.offset);
out.limit(info.offset + info.size);
muxer.writeSampleData(track, out, info);
avc.releaseOutputBuffer(index, false);
}
}
}
是的,明白我实际上是在要求您编写代码,但我没有这方面的专业知识
任何帮助表示赞赏
谢谢