如何在android中将两个音频文件组合在一起

时间:2014-07-08 12:13:06

标签: java android audio

我有2个音频文件。第一个是背景音乐,第二个是演讲。 (每个人的长度约为4分钟)

现在我想把它们混合起来,然后收到一段带有背景音乐的4分钟演讲。

2 个答案:

答案 0 :(得分:4)

您不需要FFMPEG,您可以使用Android中提供的标准编解码器。

   public void playFile(String fileToPlay)
    {
     // see where we find a suitable autiotrack
        MediaExtractor extractor = new MediaExtractor();
        try
        {
            extractor.setDataSource(fileToPlay);
        }
        catch (IOException e)
        {
            out.release();
            return;
        }
   extractor.selectTrack(0);

    String fileType=typeForFile(fileToPlay);
    if (fileType==null)
    {
        out.release();
        extractor.release();
        return;
    }

    MediaCodec codec = MediaCodec.createDecoderByType(fileType);
    MediaFormat wantedFormat=extractor.getTrackFormat(0);
    codec.configure(wantedFormat,null,null,0);
    codec.start();

    ByteBuffer[] inputBuffers = codec.getInputBuffers();
    ByteBuffer[] outputBuffers = codec.getOutputBuffers();

    // Allocate our own buffer
    int maximumBufferSizeBytes = 0;
    for(ByteBuffer bb:outputBuffers)
    {
        int c=bb.capacity();
        if (c>maximumBufferSizeBytes) maximumBufferSizeBytes=c;
    }
    setupBufferSizes(maximumBufferSizeBytes/4);

    final MediaCodec.BufferInfo bufferInfo=new MediaCodec.BufferInfo();
    MediaFormat format=null;
    while(true)
    {
        long timeoutUs=1000000;
        int inputBufferIndex = codec.dequeueInputBuffer(timeoutUs);
        if (inputBufferIndex >= 0)
        {
            ByteBuffer targetBuffer = inputBuffers[inputBufferIndex];
            int read = extractor.readSampleData(targetBuffer, 0);
            int flags=extractor.getSampleFlags();
            if (read>0)
                codec.queueInputBuffer(inputBufferIndex, 0,read, 0, flags);
            else
                codec.queueInputBuffer(inputBufferIndex, 0, 0, 0, MediaCodec.BUFFER_FLAG_END_OF_STREAM);
            extractor.advance();
        }

        int outputBufferIndex = codec.dequeueOutputBuffer(bufferInfo,timeoutUs);
        if (outputBufferIndex >= 0)
        {
            final boolean last = bufferInfo.flags == MediaCodec.BUFFER_FLAG_END_OF_STREAM;

            int s=bufferInfo.size/4;
            ByteBuffer bytes=outputBuffers[outputBufferIndex];
            ((ByteBuffer)bytes.position(bufferInfo.offset)).asShortBuffer().get(shorts,0,s*2);
            process(shorts,0,s*2);

            codec.releaseOutputBuffer(outputBufferIndex, false);
            if (last)
                break;
        }
        else if (outputBufferIndex == MediaCodec.INFO_OUTPUT_BUFFERS_CHANGED)
        {
            outputBuffers = codec.getOutputBuffers();
        }
        else if (outputBufferIndex == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED)
        {
            format = codec.getOutputFormat();
        }
    }

    extractor.release();
    codec.stop();
    codec.release();

以上代码处理1个文件的读取。 '流程'例程(尚未定义)接收交错的样本数组(左/右/左/右/左/右)。您现在需要做的就是将这些通道添加到目标缓冲区。 E.g

short[] target=new short[LENGTH OF 4 MINUTES];
int idx=0;
process(short[] audio, int l)
{
  for(int i=0;i<l;i++)
  target[idx++]+=audio[i]/2;
}

结果目标数组包含您的重叠样本。

答案 1 :(得分:0)

要合并两个音频文件,您必须使用为Android编译的FFMPEG util。 以下是我发现的有关为android编译FFMPEG的一些链接:

  1. How to Build FFmpeg for Android
  2. Download FFMPEG library for Android (SourceForge)
  3. Here是关于如何在Android中使用FFMPEG库来合并2个音频文件的Google网上论坛线程。

    另请查看this SO帖子。 希望它有所帮助。