admin管理员组

文章数量:1397102

I'm trying to use an Android MediaRecorder to record video. In order to limit the size of the file I want to periodically start a new video file, and just like the built-in Camera app I don't want to lose any frames. However, I am not reaching the point where I can even successfully test if frames are lost. Here is my workflow:

handler gets run after specified delay:

File videoFile = ...; // here is where i set the next file to output to
mediaRecorder.reset();   // i also tried calling stop() first, but the state machine diagram says this is not necessary
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mediaRecorder.setOutputFile(videoFile.getAbsolutePath());

// Set video resolution and adjust size and bitrate
mediaRecorder.setVideoSize(sharedPreferencesManager.getCameraResolution().getWidth(), sharedPreferencesManager.getCameraResolution().getHeight());
mediaRecorder.setVideoEncodingBitRate(getVideoBitrate());

// Set frame rate and capture rate
mediaRecorder.setVideoFrameRate(sharedPreferencesManager.getVideoFrameRate());
mediaRecorder.setCaptureRate(sharedPreferencesManager.getVideoFrameRate());

// Audio settings: high-quality audio
mediaRecorder.setAudioEncodingBitRate(384000);
mediaRecorder.setAudioSamplingRate(48000);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);

VideoCodec videoCodec = sharedPreferencesManager.getVideoCodec();
mediaRecorder.setVideoEncoder(videoCodec.getEncoder());

// Set orientation based on camera selection
if (sharedPreferencesManager.getCameraSelection().equals(CameraType.FRONT)) {
    mediaRecorder.setOrientationHint(270);
} else {
    mediaRecorder.setOrientationHint(90);
}

// Prepare MediaRecorder
mediaRecorder.prepare();
mediaRecorder.start();
backgroundHandler.postDelayed(this, intervalMillis);

The first video file created is playable. However, I can't get the second video file to even work. If I wait until the third video file is created I get a playable second video file that is the correct length but empty (black output, no sound, minimal size), but if I manually stop during recording of the second video I get an exception trying to call stop() telling me that stop was called while neither recording nor paused.

Yet I plainly called start(), so it's not clear to me why it's not recording.

This is running under Android 14.

本文标签: javaMediaRecorder fails to properly transition to second video fileStack Overflow