I have tested my application on various mobile phones. My applications main functionality is taking pictures and recording video through the phones camera. I didn't face an issue on most of the mobile phones, but I did face this issue on Motorola DROID RAZR. My application works fine when i take a picture. But I'm facing an issue when I record a video. When I record a video, I get a blank screen. There is no preview showing on the mobile screen. I don't why its happening, but on most of the mobile phones its working fine. And taking picture working fine on this phone (the droid razr).
Motorola DROID RAZR configuration,
- OPERATING SYSTEM : Android v2.3.5.
- CAMERA : HD camera, 8MP.
- PROCESSOR : TI OMAP4430
Here is my code,
Camera camera = Camera.open();
Parameters params = camera.getParameters();
camera.setDisplayOrientation(90);
camera.setParameters(params);
camera.setDisplayOrientation(90);
MediaRecorder recorder = new MediaRecorder();
recorder.setCamera(camera);
recorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
recorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));
recorder.setVideoSize(640, 480);
recorder.setMaxDuration(25000);
recorder.setOrientationHint(90);
Update:
I have tested this application on Motorola Droid Razr emulator. I got the following exception,
MediaRecorder(430): prepare failed: -17
System.err(430): java.io.IOException: prepare failed.
System.err(430): at android.media.MediaRecorder._prepare(Native Method)
System.err(430): at android.media.MediaRecorder.prepare(MediaRecorder.java:590)
But I didn't get this exception on my other emulator. How to resolve this issue?
Finally I found the solution. Below code works well on all devices. ;)
Holder holder = getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
Camera camera = Camera.open();
try {
camera.setPreviewDisplay(holder);
camera.startPreview();
} catch (IOException e) {
Log.e(TAG, e.getMessage());
e.printStackTrace();
}
camera.unlock();
MediaRecorder recorder = new MediaRecorder();
recorder.setCamera(camera);
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setVideoSize(640, 480);
recorder.setVideoFrameRate(20);
recorder.setVideoEncodingBitRate(3000000);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
try {
String videopath = File.createTempFile("video", ".mp4")
.getAbsolutePath();
recorder.setOutputFile(videopath);
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
recorder.setPreviewDisplay(holder.getSurface());
It appears this device does not support MediaRecorder.setOrientationHint(). Try commenting out that line. mMediaRecorder.setOrientationHint(90)
来源:https://stackoverflow.com/questions/9770986/camera-issue-with-motorola-droid-razr-when-i-recording-the-video