当我用完MediaRecorder录制视频后,函数stop()花费了我很长时间关闭文件流,我的代码怎么了?
我尝试重置有关MediaRecorder的所有配置,并检查了有关该框架的代码,本机功能停止也需要很长时间,但是当我使用其他摄像机(例如“ OpenCamera”)时,停止录制的时间很短。另外,我签出了项目“ OpenCamera”代码,并在MediaRecoder之前复制了它的配置,这对我不起作用。
这是我有关启动和停止的代码。
public void startRecord(String path) {
if (currentState.compareTo(State.previewing) == 0) {
this.mediaPath = path;
currentState = State.recording;
mediaRecorder = new MediaRecorder();
mCamera.unlock();
mediaRecorder.setCamera(mCamera);
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_LOW));
mediaRecorder.setVideoSize(videoSize.width, videoSize.height);
mediaRecorder.setOrientationHint(videoRotation);
mediaRecorder.setOutputFile(path);
File file = new File(Environment.getExternalStorageDirectory().getAbsoluteFile() + "/DCIM/Camera");
if (!file.exists()) {
file.mkdirs();
}
try {
mediaRecorder.prepare();
mediaRecorder.start();
audioManager.requestAudioFocus(audioChangeListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
} catch (Exception e) {
LogTool.e(this, "error !!!", e);
mCamera.lock();
clearException(path);
if (cameraCallback != null) {
cameraCallback.onRecordError();
}
}
}
}
public void stopRecord(boolean force) {
if (currentState.compareTo(State.recording) == 0) {
audioManager.abandonAudioFocus(audioChangeListener);
currentState = force ? State.previewing : State.showing;
if (mediaRecorder != null) {
try {
mediaRecorder.stop();
mediaRecorder.reset();
mediaRecorder.release();
mediaRecorder = null;
} catch (Exception e) {
clearException(mediaPath);
if (!force) {
Toast.makeText(mContext, mContext.getResources().getString(R.string.video_tool_short),
Toast.LENGTH_SHORT).show();
}
if (cameraCallback != null) {
cameraCallback.onRecordError();
}
return;
}
if (force) {
File errorFile = new File(mediaPath);
if (errorFile.exists()) {
errorFile.delete();
}
if (cameraCallback != null) {
cameraCallback.onRecordError();
}
} else {
if (cameraCallback != null) {
cameraCallback.onMediaSaved(mediaPath);
}
}
}
}
}
有关该框架的日志显示在其中:
01-01 13:32:08.650 4129-4129/? D/MediaRecorder: start begin
01-01 13:32:09.150 4129-4129/? D/MediaRecorder: start end
01-01 13:32:12.620 4129-4129/? D/MediaRecorder: stop start
01-01 13:32:17.770 4129-4129/? D/MediaRecorder: stop end
您可以看到系统日志显示需要5秒钟才能停止该操作。
答案 0 :(得分:0)
可能是设备错误。当我设置setRecordingHint(true)时,它对我来说可以解决问题。