2017-02-08 34 views
1

下面是Android的MediaMuxer API示例代码片段: https://developer.android.com/reference/android/media/MediaMuxer.htmlgetInpuBuffer Android中MediaCodec和MediaMuxer

MediaMuxer muxer = new MediaMuxer("temp.mp4", OutputFormat.MUXER_OUTPUT_MPEG_4); 
// More often, the MediaFormat will be retrieved from MediaCodec.getOutputFormat() 
// or MediaExtractor.getTrackFormat(). 
MediaFormat audioFormat = new MediaFormat(...); 
MediaFormat videoFormat = new MediaFormat(...); 
int audioTrackIndex = muxer.addTrack(audioFormat); 
int videoTrackIndex = muxer.addTrack(videoFormat); 
ByteBuffer inputBuffer = ByteBuffer.allocate(bufferSize); 
boolean finished = false; 
BufferInfo bufferInfo = new BufferInfo(); 

muxer.start(); 
while(!finished) { 
    // getInputBuffer() will fill the inputBuffer with one frame of encoded 
    // sample from either MediaCodec or MediaExtractor, set isAudioSample to 
    // true when the sample is audio data, set up all the fields of bufferInfo, 
    // and return true if there are no more samples. 
    finished = getInputBuffer(inputBuffer, isAudioSample, bufferInfo); 
    if (!finished) { 
    int currentTrackIndex = isAudioSample ? audioTrackIndex : videoTrackIndex; 
    muxer.writeSampleData(currentTrackIndex, inputBuffer, bufferInfo); 
    } 
}; 
muxer.stop(); 
muxer.release(); 

对于此行:finished = getInputBuffer(inputBuffer, isAudioSample, bufferInfo);我没有找到这个功能getInputBuffer两个MediaCodec.java和MediaMuxer。 java,是用户定义的函数还是API函数?

回答

0

在这种情况下,getInputBuffer是一个假设的用户定义函数。它不是一个API函数。上面的评论解释了它应该做什么。 (请注意,它如何实际上不会以写入的方式工作,因为isAudioSample变量无法以正确写入的方式被函数更新。)

+0

假设我想记录视频(从表面)和使用MediaCodec和MediaMuxer API的音频,我应该明确设置bufferInfo(包括视频和音频)的时间戳还是只使用默认值?也许某些平台对曲面和音频使用不同的时间戳,例如开机时间和单调时间等。 – mewo1234

+0

假设您有适当的输入时间戳到编码器,我认为mediacodec的时间戳应该可以正常工作。 – mstorsjo

+0

@ mewo1234当使用Surface输入到MediaCodec时,您应该手动设置时间戳,因为您无法将时间戳与来自Surface的MediaCodec输入上的帧相关联。根据我的经验,如果不这样做可能会导致一些跳帧。这意味着您需要为编码器的输出缓冲区设置缓冲区信息'presentationTimeUs'。你不需要担心音频的时间戳; MediaCodec唯一关心输入音频时间戳的是他们正在严格增加。 MediaCodec应该使用其编码的音频样本输出正确的时间戳。 – nyttimangus

相关问题