gpt4 book ai didi

java - 当 setMaxFileSize() 和 setMaxDuration() 组合使用时 MediaRecorder 服务器崩溃

转载 作者:行者123 更新时间:2023-12-01 22:07:35 29 4
gpt4 key购买 nike

在设置 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

最佳答案

不幸的是,SetMaxDuration() 不具有与 SetMaxFileSize 相同的功能(可用于设置连续分块记录的新文件名)。但是,如果您不需要精确的最大持续时间,则可以根据音频和视频比特率以及一些开销来估计给定最大持续时间的最大文件大小。

关于java - 当 setMaxFileSize() 和 setMaxDuration() 组合使用时 MediaRecorder 服务器崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58680168/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com