首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android,向Muxer添加音频曲目

Android,向Muxer添加音频曲目
EN

Stack Overflow用户
提问于 2014-12-18 20:31:01
回答 1查看 1.8K关注 0票数 1

我正在尝试添加音频到以下开源项目创建的视频

特别是针对https://github.com/madisp/trails/blob/master/app/src/main/java/com/madisp/trails/CaptureService.java

我需要从麦克风获得音频,并将其作为音轨写入编码文件。目前,用Muxer编码的文件只有视频轨道。

我可以从MIC获得音频,下面没有任何问题

代码语言:javascript
复制
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)中

代码语言:javascript
复制
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);
        }
    }
}

是的,理解我的字面意思是请你来写代码,但我没有这方面的专业知识

感谢您的任何帮助

谢谢

EN

回答 1

Stack Overflow用户

发布于 2014-12-27 06:35:11

首先,对于AudioRecord使用的缓冲区,使用byte[]而不是short[] -这会稍微简化一些。

然后,要对接收到的缓冲区进行编码,应该可以使用下面的代码(未测试):

代码语言:javascript
复制
while (recording) {
    readSize = aRecorder.read(buffer, 0, minBufferSize);
    if (readSize < 0) {
        break;
    } else if (readSize > 0) {
        boolean done = false;
        while (!done) {
            int index = avc.dequeueInputBuffer(10000);
            if (index >= 0) { // In case we didn't get any input buffer, it may be blocked by all output buffers being full, thus try to drain them below if we didn't get any
                ByteBuffer in = avc.getIndexBuffer(index);
                in.clear();
                in.put(buffer, 0, readSize);
                avc.queueInputBuffer(index, 0, readSize, System.nanoTime()/1000, 0);
                done = true; // Done passing the input to the codec, but still check for available output below
            }
            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 && info.size > 0) {
                    ByteBuffer out = avc.getOutputBuffer(index);
                    out.position(info.offset);
                    out.limit(info.offset + info.size);
                    muxer.writeSampleData(track, out, info);
                    avc.releaseOutputBuffer(index, false);
                }
            }
        }
    }
}

我认为普通的SW AAC编码器应该可以传递任意字节的音频给它,但如果编码器很挑剔,你需要传递1024个样本的块记录数据(单声道2048字节,立体声4096字节)。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27546678

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档