How to avoid missing frame when recording chunks with MediaRecorder

Viewed 264

I want to record video chunks in Android using the MediaRecorder. I would like to produce a small "playable" video chunks instead of a big one in the end of the recording session. I tried to using the setNextOutputFile API with addition to the setOnInfoListener event. using this code:

@Override
public void onInfo(MediaRecorder mediaRecorder, int what, int extra) {
    Log.d(TAG, "mediaRecorder onInfo, what:" + what + "  extra:" + extra + "  recorderIndex:"+recorderIndex);
    switch (what) {
        case MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED:
            return;
        case MediaRecorder.MEDIA_RECORDER_INFO_MAX_FILESIZE_APPROACHING:
            File nextFile2 = new File(getNextChunkPath());
            try {
                mediaRecorder.setNextOutputFile(nextFile2);
            } catch (IOException e) {
                e.printStackTrace();
            }
            return;
        case MediaRecorder.MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED:
            return;
        case MediaRecorder.MEDIA_RECORDER_INFO_NEXT_OUTPUT_FILE_STARTED:
            return;
    }
}

This worked well, but the main problem is that between every chunk I have a one missing frame that isn't recorded (compering to the normal recording).

I tried also to do a workaround with 2 MediaRecorders, setting 1 in pause mode while the other is working. This gave me chunks that have 1-2 identical frmaes (in the end of chunk X and in the start of chunk X+1):

@Override
public void onInfo(MediaRecorder mediaRecorder, int what, int extra) {
    Log.d(TAG, "mediaRecorder onInfo, what:" + what + "  extra:" + extra + "  recorderIndex:"+recorderIndex);
    switch (what) {
        case MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED:
            return;
        case MediaRecorder.MEDIA_RECORDER_INFO_MAX_FILESIZE_APPROACHING:
            File nextFile2 = new File(getNextChunkPath());
            try {
                mediaRecorder.setNextOutputFile(nextFile2);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if(recorderIndex % 2 == 1){
                if(recorderIndex == 1){
                    mMediaRecorder2.start();
                }
                else {
                    mMediaRecorder2.resume();
                }
            }
            else{
                mMediaRecorder.resume();
            }
            recorderIndex++;
            return;
        case MediaRecorder.MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED:
            return;
        case MediaRecorder.MEDIA_RECORDER_INFO_NEXT_OUTPUT_FILE_STARTED:
            mediaRecorder.pause();
            return;
    }
}

How can I make this MediaRecorder to chunk the video without any frame lost?

My full file can be found here

0 Answers
Related