I call this method and it gives me a camera object is a null pointer on the recorder.setCamera(this.camera) call
private void initRecorder()
{
if(camera == null)
{
camera = Camera.open();
camera.unlock();
}
recorder.setCamera(this.camera);
recorder.setPreviewDisplay(holder.getSurface());
recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
recorder.setVideoFrameRate(60);
CamcorderProfile cpHigh = CamcorderProfile.get(cameraInfo.facing,CamcorderProfile.QUALITY_HIGH);
recorder.setProfile(cpHigh);
String fileName = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());
recorder.setOutputFile(varPtr.DefaultStorageDir + fileName + ".mp4");
recorder.setMaxDuration(50000); // 50 seconds
//recorder.setMaxFileSize(5000000); // Approximately 5 megabytes
}
it is called from my onSurfaceCreated function seen here
@Override
public void surfaceCreated(SurfaceHolder holder)
{
Log.v(LOGTAG, "surfaceCreated");
initRecorder();
}
I have this in my manifest
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-feature android:name="android.hardware.camera.front" />
<uses-feature android:name="android.hardware.camera.back" />
<uses-feature android:name="android.hardware.microphone"/>
<uses-permission android:name="android.permission.RECORD_AUDIO"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE"></uses-permission>
I even tried using this code which should definitely work because I have selected in my avd settings to emulate both the front and back cameras so they are emulated. I even tested the emulator's default camera app and the video and still camera work on it.
private Camera openFrontFacingCamera()
{
int cameraCount = 0;
Camera cam = null;
cameraCount = Camera.getNumberOfCameras();
for (int camIdx = 0; camIdx < cameraCount; camIdx++) {
Camera.getCameraInfo(camIdx, cameraInfo);
if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
try {
cam = Camera.open(camIdx);
} catch (RuntimeException e) {
Log.e("WTF", "Camera failed to open: " + e.getLocalizedMessage());
}
}
}
return cam;
}
please if anyone is experienced with android help! I'm really starting to absolutely despise developing for android.
I will try it even without your LogCat
and additional Code from your side.
For me it Looks like your recorder
isn't initialized. That's why it throws a NPE at this line.
Make sure you are initializing recorder
before Setting a CameraObject
to recorder
.
Because regarding to the docs
:
RuntimeException
if opening the camera fails (for example, if the camera is in use by another process or device policy manager has disabled the camera).
so your camera-object
should be valid... so the only Logical answere is your recorder
isn't initialized.
Read more here: http://developer.android.com/reference/android/hardware/Camera.html#open(int)
Hope it helps!