Mediarecorder Start Failed: -16
So I realize there are many solutions out there for MediaRecorder start errors, however most of them revolve around 'start failed: -19', which has been linked to 'NO_INIT' See comm
Solution 1:
I ran in to this error (-16 on start) and found out it was caused by using an unsupported resolution.
First you have to get the optimal size from the supported sizes
Parameters params = camera.getParameters();
List<Size> sizes = params.getSupportedPreviewSizes();
optimalSize = getOptimalPreviewSize(sizes, width, height);
params.setPreviewSize(optimalSize.width, optimalSize.height);
Then make sure you set both preview and video to the same size (if they were different in my experience preview would freeze when video record started):
mediaRecorder.setVideoSize(optimalSize.width, optimalSize.height);
(sample code for getOptimalPreviewSize is from android sdk)
private Size getOptimalPreviewSize(List<Size> sizes, int w, int h){
finaldouble ASPECT_TOLERANCE = 0.1;
double targetRatio = (double) w / h;
if (sizes == null) return null;
Size optimalSize = null;
double minDiff = Double.MAX_VALUE;
int targetHeight = h;
// Try to find an size match aspect ratio and sizefor (Size size : sizes) {
double ratio = (double) size.width / size.height;
if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue;
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
// Cannot find the one match the aspect ratio, ignore the requirementif (optimalSize == null) {
minDiff = Double.MAX_VALUE;
for (Size size : sizes) {
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
}
return optimalSize;
}
Post a Comment for "Mediarecorder Start Failed: -16"