I have read everything about MediaRecorder and followed the GUIDES on Android.
Well, it supose to work fine at least on API 8 and higher, but my minimum SDK is 14, so I it should be better.
On my device, Samsung Galaxy S3 Mini it works fine. Some other devices with Ice Cream Sandwitch as well, but some others not. They crash on:
mediaRecorder.start()
I create a service for recording video and this is my code (guide for what I do on every stage):
@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
//OPEN CAMERA
int ncamara=0;
qcamara=0;
if(android.os.Build.VERSION.SDK_INT<9)
camera = Camera.open();
else{
try {
ncamara = Camera.getNumberOfCameras();
if (f.leer("camara", context).equals("frontal"))
qcamara=10;
else qcamara=20;
} catch (Exception e) {}
if (ncamara>=2 && qcamara == 10)
camera = Camera.open(Camera.CameraInfo.CAMERA_FACING_FRONT);
else if (ncamara>=2 && qcamara == 20)
camera = Camera.open(Camera.CameraInfo.CAMERA_FACING_BACK);
else
camera = Camera.open();
}
//
try {
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
} catch (IOException e1) {
e1.printStackTrace();
}
//UNLOCK CAMERA
camera.unlock();
mediaRecorder = new MediaRecorder();
mediaRecorder.setCamera(camera); //setCamera
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
//setProfile
if (f.leer("calidad", context).equals("alta") && qcamara !=10)
mediaRecorder.setProfile(CamcorderProfile.get(0, CamcorderProfile.QUALITY_HIGH));
else if (f.leer("calidad", context).equals("alta") && qcamara == 10)
mediaRecorder.setProfile(CamcorderProfile.get(1, CamcorderProfile.QUALITY_LOW));
else if (f.leer("calidad", context).equals("baja") && qcamara != 10)
mediaRecorder.setProfile(CamcorderProfile.get(0, CamcorderProfile.QUALITY_LOW));
else
mediaRecorder.setProfile(CamcorderProfile.get(1, CamcorderProfile.QUALITY_LOW));
mediaRecorder.setVideoSize(320, 240);
if (CameraRecorder.duracion>0) // -- duration
mediaRecorder.setMaxDuration(CameraRecorder.duracion * 1000);
File dataDirectory = Environment.getExternalStorageDirectory();
File fileDir = new File(dataDirectory, "/weddingR/");
fileDir.mkdirs();
mediaRecorder.setOutputFile( //setOutputFile
Environment.getExternalStorageDirectory()+"/weddingR/"+
DateFormat.format("yyyy-MM-dd---kk-mm-ss", new Date().getTime())+
".mp4"
);
mediaRecorder.setPreviewDisplay(surfaceHolder.getSurface());
if (qcamara==10){ // FRONT CAMERA
try { mediaRecorder.setOrientationHint(270); } catch (Exception e) {}
} else { // BACK CAMERA
try { mediaRecorder.setOrientationHint(90); } catch (Exception e) {}
}
try {
mediaRecorder.prepare();
} catch (Exception e) {}
mediaRecorder.setOnInfoListener(new OnInfoListener(){
@Override
public void onInfo(MediaRecorder mr, int what, int extra) {
Log.d("INFO_ON", what + " " + extra + " ");
nascosto=true;
stopSelf();
}
});
mediaRecorder.start();
}
If I set mediaRecorder.setVideoFrameRate(20) for example, it crashes on every device.
I'm most asking before in some devices works fine, but not others (for example, EMULATOR with API 17 or 18 works... devices with API 15 works but EMULATOR API 15 doesn't work...)
What am I doing wrong ??
Thanks in advance.
ANDROID MANIFEST PERMISSIONS
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
EDIT
After @Melquiades answer, I edited my code now catching exceptions and no exception is caught.
Also, I'd like to point some interesting thing: I'm trying with API15 emulator and this happens:
- When I set
mediaRecorder.setPreviewDisplay(surfaceHolder.getSurface());(aftermediaRecorder.setOutputFile()), I getSTART FAILED -19 - When I don't set it, I get
START FAILED -2147483648
But in my Samsung it still works fine...
NEW EDIT (01/01/2014) HAPPY NEW YEAR!!
I tried on Samsung Galaxy S2 Plus (Android 4.1.2) and it doesn't work, but on mine, Samsung Galaxy S3 Mini it works (also Android 4.1.2)... I'm becoming crazy.