Android MediaMuxer readSampleData IllegalStateException

倾然丶 夕夏残阳落幕 提交于 2019-12-10 11:58:47

问题


I am using the following code to generate a video. However, one in every 5 attempts the readSampleData raises either a IllegalArgumentException or a IllegalStateException. What can I do to stop the exceptions. Can I check the state of the extractor before calling readSampleData. If so, how?

MediaExtractor videoExtractor = new MediaExtractor();
videoExtractor.setDataSource(SOURCE);

Log.d(TAG, "Video Extractor Track Count " + videoExtractor.getTrackCount() );

MediaMuxer muxer = new MediaMuxer(outputFile, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);

videoExtractor.selectTrack(0);
MediaFormat videoFormat = videoExtractor.getTrackFormat(0);
int videoTrack = muxer.addTrack(videoFormat);

Log.d(TAG, "Video Format " + videoFormat.toString() );

boolean videoEnded = false;
long videoTimeStamp = 0;
int frameCount = 0;
int offset = 100;
ByteBuffer videoBuf = ByteBuffer.allocate(MAX_SAMPLE_SIZE);
BufferInfo videoBufferInfo = new BufferInfo();

muxer.start();

while (!videoEnded) 
{
    videoBufferInfo.offset = offset;
    videoBufferInfo.size = videoExtractor.readSampleData(videoBuf, offset);

    if (videoBufferInfo.size < 0) 
    {
        Log.d(TAG, "video ended " + videoBufferInfo.size);
        videoEnded = true;
        videoBufferInfo.size = 0;
    } 
    else 
    {
        videoTimeStamp = videoBufferInfo.presentationTimeUs = videoExtractor.getSampleTime();
        videoBufferInfo.flags = videoExtractor.getSampleFlags();
        muxer.writeSampleData(videoTrack, videoBuf, videoBufferInfo);
        videoExtractor.advance();
        frameCount++;
        Log.d(TAG, "Frame (" + frameCount + ") Video PresentationTimeUs:" + (videoBufferInfo.presentationTimeUs/1000000.0f)  +" Flags:" + videoBufferInfo.flags +" Size " + videoBufferInfo.size);
    }
}

muxer.stop();
muxer.release();

The actual code adds audio to the video after the video extractor loop.

来源:https://stackoverflow.com/questions/26463802/android-mediamuxer-readsampledata-illegalstateexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!