问题
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