当我设置MediaRecorder实例并且结合使用setMaxFileSize和setMaxDuration时,收到消息“ W / IMediaDeathNotifier:媒体服务器已死”,并且记录停止。我要完成的工作是重复录制,直到达到最大持续时间。
当我单独使用这些方法时,它们可以很好地工作。我可以设置持续时间或文件大小,并让onInfoListener进行回调。由于我可以做到这一点,所以我自己做了一个持续时间计时器,有时会导致出现诸如相机缓冲区错误之类的问题。
这是代码 https://pastebin.com/J4aSrEZU
private void prepareMediaRecorderV26() {
mMediaRecorder = new MediaRecorder();
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mMediaRecorder.setVideoEncodingBitRate(10000000);
mMediaRecorder.setVideoFrameRate(30);
mMediaRecorder.setVideoSize(cameraResolution.getWidth(), cameraResolution.getHeight());
Log.i(TAG, "Width:"+ cameraResolution.getWidth()+" Height:"+ cameraResolution.getHeight());
mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
// Save location and file
@SuppressLint("SimpleDateFormat")
final String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
final File outputFile = presetRecordingFile(recordingDirectory, 1, timeStamp);
mMediaRecorder.setOutputFile(outputFile.getAbsolutePath());
mMediaRecorder.setMaxFileSize(recordingFileSize);
mMediaRecorder.setMaxDuration(120000);
mMediaRecorder.setOrientationHint(cameraOrientation);
//Todo: native max duration leads to MediaServer dying
//mMediaRecorder.setMaxDuration(recordingDuration);
//mMediaRecorder.setMaxFileSize(recordingFileSize);
//Listen to file size and other relevant info to continue recording
mMediaRecorder.setOnInfoListener(new MediaRecorder.OnInfoListener() {
int i = 2;
String startTimeStamp = timeStamp;
File currentOutputFile = outputFile;
private String TAG = "MediaRecorder info listener";
@SuppressLint("NewApi")
@Override
public void onInfo(MediaRecorder mr, int what, int extra) {
switch (what) {
case MediaRecorder.MEDIA_RECORDER_INFO_MAX_FILESIZE_APPROACHING:
try {
currentOutputFile = presetRecordingFile(recordingDirectory, i, startTimeStamp);
mr.setNextOutputFile(currentOutputFile);
i++;
} catch (IOException e) {
e.printStackTrace();
}
Log.i(TAG, "Max file size approaching");
break;
case MediaRecorder.MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED:
Log.i(TAG, "Max file size reached");
stopRecording();
break;
case MediaRecorder.MEDIA_RECORDER_INFO_NEXT_OUTPUT_FILE_STARTED:
Log.i(TAG, "OutputFile: " + currentOutputFile);
if (vibrationStartRecordingEnabled && vibrationStartOfEachFileEnabled) {
vibrator.vibrate(VibrationEffect.createOneShot(500, VibrationEffect.DEFAULT_AMPLITUDE));
}
break;
}
}
});
mMediaRecorder.setOnErrorListener(new MediaRecorder.OnErrorListener() {
@Override
public void onError(MediaRecorder mr, int what, int extra) {
Log.i(TAG, "ERROR: "+ what);
if (what == MediaRecorder.MEDIA_ERROR_SERVER_DIED) {
Log.i(TAG, "MediaRecorder.MEDIA_ERROR_SERVER_DIED");
MainActivity.this.stopRecording();
}
}
});
try {
mMediaRecorder.prepare();
} catch (IOException e) {
e.printStackTrace();
}
}
这是日志
V/Service: CREATE FOLDER: CHECK
FOLDER EXISTS: true
I/Service: onOpened
VERSION 26
I/MediaRecorderJNI: setup
I/MediaRecorderJNI: setAudioSource(5)
I/MediaRecorderJNI: setVideoSource(2)
I/MediaRecorderJNI: setParameter()
setVideoFrameRate(30)
setVideoSize(4032, 3024)
I/Service: Width:4032 Height:3024
I/MediaRecorderJNI: setVideoEncoder(2)
I/MediaRecorderJNI: setAudioEncoder(3)
W/MediaRecorder: setOutputFile: path = /storage/emulated/0/DCIM/Camera/file_20191103_132850_1.mp4
E/Media_APM :: isCalledPackage return false
I/MediaRecorderJNI: setMaxFileSize(36700160)
setMaxDuration(120000)
I/MediaRecorderJNI: setParameter()
W/MediaRecorder: prepare: mPath = /storage/emulated/0/DCIM/Camera/file_20191103_132850_1.mp4
I/MediaRecorderJNI: setOutputFile
prepare
I/MediaRecorderJNI: getSurface
I/Service: onConfigured
I/MediaRecorderJNI: start
I/MediaRecorder info listener: Max file size approaching
I/MediaRecorder info listener: OutputFile: /storage/emulated/0/DCIM/Camera/file_20191103_132850_2.mp4
W/IMediaDeathNotifier: media server died
I/Service: ERROR: 100
MediaRecorder.MEDIA_ERROR_SERVER_DIED
答案 0 :(得分:0)
不幸的是,SetMaxDuration()不具有与SetMaxFileSize相同的功能(可用于设置新的文件名以进行连续的分块记录)。但是,如果不需要精确的最大持续时间,则可以根据音频和视频双速率来估算给定最大持续时间的最大文件大小,再加上一些开销。