Android MediaRecorder - “启动失败:-19”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10496969/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android MediaRecorder - "start failed: -19"
提问by 0605002
I'm trying to create a video recorder on Android, and I've prepared my code which is supposed to be working - but I constantly get an error message start failed: -19
.
我正在尝试在 Android 上创建一个录像机,并且我已经准备好了应该可以工作的代码 - 但我经常收到一条错误消息start failed: -19
。
Here's my code:
这是我的代码:
public boolean startRecording() {
try {
camera.unlock();
mediaRecorder = new MediaRecorder();
mediaRecorder.setOnErrorListener(new MediaRecorder.OnErrorListener() {
@Override
public void onError(MediaRecorder mr, int what, int extra) {
Log.i(TAG, "Error");
}
});
mediaRecorder.setCamera(camera);
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
Log.i(TAG, "a");
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263);
Log.i(TAG, "b");
mediaRecorder.setMaxDuration(maxDurationInMs); // set to 20000
String uniqueOutFile = OUTPUT_FILE + System.currentTimeMillis() + ".3gp";
File outFile = new File(uniqueOutFile);
if (outFile.exists()) {
outFile.delete();
}
mediaRecorder.setOutputFile(uniqueOutFile);
mediaRecorder.setVideoFrameRate(videoFramesPerSecond); // set to 20
mediaRecorder.setVideoSize(sView.getWidth(), sView.getHeight());
Log.i(TAG, "c");
mediaRecorder.setPreviewDisplay(holder.getSurface());
mediaRecorder.setMaxFileSize(maxFileSizeInBytes); // set to 50000
mediaRecorder.prepare();
Log.i(TAG, "d");
mediaRecorder.start();
Log.i(TAG, "e");
return true;
} catch (IllegalStateException e) {
Log.i(TAG, "f");
Log.e(TAG, e.getMessage());
e.printStackTrace();
camera.lock();
return false;
} catch (IOException e) {
Log.i(TAG, "g");
Log.e(TAG, e.getMessage());
e.printStackTrace();
camera.lock();
return false;
} catch (RuntimeException e) {
Log.i(TAG, "h");
Log.e(TAG, e.getMessage());
camera.lock();
return false;
}
}
All the debug logs (from "a" through "d") are printed in log, so it seems that all the steps upto mediaRecorder.prepare()
are properly done. Then it catches a RuntimeException
with message start failed: -19
. There is a similar question, but that doesn't solve my problem.
所有调试日志(从“a”到“d”)都打印在日志中,因此似乎所有步骤mediaRecorder.prepare()
都正确完成。然后它捕获一个RuntimeException
with 消息start failed: -19
。有一个类似的问题,但这并不能解决我的问题。
Is there any other reason to get such an error?
有没有其他原因会出现这样的错误?
回答by 0605002
Just found out the bug, in this line:
刚刚发现了这个错误,在这一行:
mediaRecorder.setVideoSize(sView.getWidth(), sView.getHeight());
after commenting out this line, the code runs perfectly!
注释掉这一行后,代码运行完美!
回答by Sheraz Ahmad Khilji
I solved my problem once i added this for video recording
一旦我添加了这个用于视频录制,我就解决了我的问题
/**
* Start video recording by cleaning the old camera preview
*/
private void startVideoRecorder() {
// THIS IS NEEDED BECAUSE THE GLASS CURRENTLY THROWS AN ERROR OF
// "MediaRecorder start failed: -19"
// THIS WONT BE NEEDED INCASE OF PHONE AND TABLET
// This causes crash in glass kitkat version so remove it
// try {
// mCamera.setPreviewDisplay(null);
// } catch (java.io.IOException ioe) {
// Log.d(TAG,
// "IOException nullifying preview display: "
// + ioe.getMessage());
// }
// mCamera.stopPreview();
// mCamera.unlock();
recorder = new MediaRecorder();
// Let's initRecorder so we can record again
initRecorder();
}
/**
* Initialize video recorder to record video
*/
private void initRecorder() {
try {
File dir = new File(folderPath);
if (!dir.exists()) {
dir.mkdirs();
}
mCamera.stopPreview();
mCamera.unlock();
videofile = new File(dir, fileName + ".mp4");
recorder.setCamera(mCamera);
// Step 2: Set sources
recorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
// Step 3: Set a CamcorderProfile (requires API Level 8 or higher)
recorder.setProfile(CamcorderProfile
.get(CamcorderProfile.QUALITY_HIGH));
// Step 4: Set output file
recorder.setOutputFile(videofile.getAbsolutePath());
// Step 5: Set the preview output
recorder.setPreviewDisplay(mPreview.getHolder().getSurface());
// Step 6: Prepare configured MediaRecorder
recorder.setMaxDuration(video_duration * 1000);
recorder.setOnInfoListener(new OnInfoListener() {
@Override
public void onInfo(MediaRecorder mr, int what, int extra) {
if (what == MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED) {
mCamera.stopPreview();
releaseMediaRecorder();
/*
* initiate media scan and put the new things into the
* path array to make the scanner aware of the location
* and the files you want to see
*/MediaScannerConnection.scanFile(
CuxtomCamActivity.this,
new String[] { videofile.getPath() }, null,
null);
Intent intent = new Intent();
intent.putExtra(CuxtomIntent.FILE_PATH,
videofile.getPath());
intent.putExtra(CuxtomIntent.FILE_TYPE, FILE_TYPE.VIDEO);
setResult(RESULT_OK, intent);
finish();
}
}
});
recorder.prepare();
recorder.start();
} catch (Exception e) {
Log.e("Error Stating CuXtom Camera", e.getMessage());
}
}
private void releaseMediaRecorder() {
if (recorder != null) {
recorder.reset(); // clear recorder configuration
recorder.release(); // release the recorder object
recorder = null;
}
}
For detailed guide refer to this Open Source Cuxtom Cam
有关详细指南,请参阅此开源 Cuxtom Cam
回答by Adnan Abdollah Zaki
the problem is in your setVideoSize()
code .
问题出在您的setVideoSize()
代码中。
and why this code error ...
以及为什么这个代码错误......
From the research I have done, error code -19 comes about when there is a problem with the size of the video as set by MediaRecorder#setVideoSize()
根据我所做的研究,当设置的视频大小出现问题时,会出现错误代码 -19 MediaRecorder#setVideoSize()
run this code , and see whitch screen that your camera in your device can support :
运行此代码,并查看设备中的相机可以支持的屏幕:
final List<Camera.Size> mSupportedVideoSizes = getSupportedVideoSizes(mCamera);
for (Camera.Size str : mSupportedVideoSizes)
Log.e(TAG, "mSupportedVideoSizes "+str.width + ":" + str.height + " ... "
+ ((float) str.width / str.height));
and method is :
方法是:
public List<Size> getSupportedVideoSizes(Camera camera) {
if (camera.getParameters().getSupportedVideoSizes() != null) {
return camera.getParameters().getSupportedVideoSizes();
} else {
// Video sizes may be null, which indicates that all the supported
// preview sizes are supported for video recording.
return camera.getParameters().getSupportedPreviewSizes();
}
}
回答by Rafael Matos
I had that problem with some specific phones, I've found out that I couldn't set camcoder profile sizes in some of them. But when that worked for the problematic androids it stopped working on the previous working devices.
我在某些特定手机上遇到了这个问题,我发现我无法在其中一些手机中设置摄像机配置文件大小。但是当它适用于有问题的机器人时,它停止在以前的工作设备上工作。
So in the end my implemented logic was something like:
所以最后我实现的逻辑是这样的:
- Set width/height
- Try to start the merdia recorder
- In case of exception, try again without setting width/height
- 设置宽度/高度
- 尝试启动 merdia 记录器
- 如有异常,请重试而不设置宽度/高度
Kind of a trash logic, but that worked.
有点垃圾逻辑,但确实有效。
I've setup a github project with that implementation, try it out: https://github.com/rafaelsilverio/MediaRecorder
我已经使用该实现设置了一个 github 项目,试试看:https: //github.com/rafaelsilverio/MediaRecorder
回答by ConanZhi
I also encountered this problem and annotated the following two ways, because the hardware does not support the two configurations.
我也遇到这个问题,注释了以下两种方式,因为硬件不支持这两种配置。
MediaRecorder .setVideoSize()
MediaRecorder .setVideoFrameRate()