I'm trying to record video from the Camera using the MediaRecorder. Here's a code snippet
snip..
mr.setAudioSource( MediaRecorder.AudioSource.MIC );
mr.setVideoSource( MediaRecorder.VideoSource.CAMERA);
mr.setOutputFormat( MediaRecorder.OutputFormat.THREE_GPP );
mr.setAudioEncoder( MediaRecorder.AudioEncoder.AMR_NB );
mr.setVideoEncoder( MediaRecorder.VideoEncoder.MPEG_4_SP );
mr.setVideoSize( 200, 200 );
mr.setVideoFrameRate( 15 );
..snap
Code executes on a MileStone/Droid, non-empty output file will be created. But when I try to view the video, it looks like this:
My first thoughts were about some sort of encoding error, so I tried every possible OutputFormat/VideoEncoder combination, with no effetcs on the result.
LogCat shows the following error
CameraInput: Unsupported parameter(x-pvmf/media-input-node/cap-config-interface;valtype=key_specific_value)
But I can't figure out, what I may have set wrong. I used camera.getParameters(), set the preview size with the returned params and then pushed them back using camera.setParameters()...
Worked thru every piece of sample code I could find, but still found no solution.
Does anyone have any ideas ?
you must set the correct setVideoSize( x, y) function.
you must call the function which give you the size options , and choose from that list
when camera open,
Camera.Parameters p = mCamera.getParameters();
p.setPreviewSize(mSur.getWidth(), mSur.getHeight());
mCamera.setParameters(p);
and when you prepareRecord
mCamera.unlock();
if (mRecorder == null) {
mRecorder = new MediaRecorder();
} else {
mRecorder.reset();
}
mNextRecordFileName = getOneFileName();
mRecorder.setCamera(mCamera);
mRecorder.setVideoSource(mVideoSource);
mRecorder.setAudioSource(mAudioSource);
mRecorder.setPreviewDisplay(mSur.getHolder().getSurface());
mRecorder.setOutputFormat(mVideoFormat);
//设置在setEncoder之前才有效,如果不设置,htc会崩溃掉
mRecorder.setVideoSize(this.mSur.getWidth(), mSur.getHeight());
Log.i(TAG, this.mSur.getHolder().getSurfaceFrame().height()+"gao");
Log.i(TAG, this.mSur.getHolder().getSurfaceFrame().width()+"kuan");
Log.i(TAG, "宽"+this.mSur.getWidth()+"高"+mSur.getHeight());
mRecorder.setVideoEncoder(mVideoEncoder);
mRecorder.setAudioEncoder(mAudioEncoder);
mRecorder.setOutputFile(mNextRecordFileName);
hope it can help you
To avoid distortion in recorded video ( I have seen this on Galaxy S3) make sure you set camera parameter preview size and mediarecorder videosize to same height and width.
To get supported camera preview size:
Camera.Parameters parameters = camera.getParameters();
List<Camera.Size> list = parameters.getSupportedPreviewSizes();
parameters.setPreviewSize(list.get(X).width, list.get(X).height);
// If you set Width and Height not supported by device you will get exception
// MediaRecorder object mMediaRecoder
mMediaRecoder.setVideoSize(list.get(X).width, list.get(X).height);
Hey,
I know you posted this a while ago and I doubt your still looking for an answer but I thought this might help someone else out.
I think your problem is mr.setVideoSize( 200, 200 );
I doubt the phones camera supports a 1X1 capture resolution. It is better to use something like mr.setVideoSize(Camcorder.get(Camcorder.QUALITY_LOW).videoFrameWidth,Camcorder.get(Camcorder.QUALITY_LOW).videoFrameHeight);
That will ensure that the resolution is supported by the camera. Also make sure your preview resolution matches your camera resolution, or that can cause the same problem. I know it happens to me if I have my preview set to QUALITY_HIGH and my camera to QUALITY_LOW
Related
This is coded in NativeScript, so I'll try my best to adapt the scenario to Java. I have created an in-app video view with support to record the video.
This is done as follows:
First I create a SurfaceView that will hold the preview of the camera:
this.mSurfaceView = new android.view.SurfaceView(this._context);
this.mHolder = this.mSurfaceView.getHolder();
this.mHolder.setType(android.view.SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
Then I create an instance of the Camera, and sets the video surface:
var mCamera = android.hardware.Camera;
var camera = mCamera.open(1);
this.camera = camera;
this.camera.setDisplayOrientation(90);
var parameters = camera.getParameters();
parameters.setRecordingHint(true);
if( parameters.isVideoStabilizationSupported() ){
parameters.setVideoStabilization(true);
}
camera.setParameters(parameters);
this.camera.setPreviewDisplay(_this.mHolder);
this.camera.startPreview();
this.camera.startFaceDetection();
Now, all is good. I have the camera preview in the view that I want it to be. The color is good and I think the image aspect ratio is good too.
However, when I initiate the recording, as I do with the following code:
this.mediarecorder = new android.media.MediaRecorder();
// Step 1: Unlock and set camera to MediaRecorder
this.camera.unlock();
this.mediarecorder.setCamera(this.camera);
// Step 2: Set sources
this.mediarecorder.setAudioSource(android.media.MediaRecorder.AudioSource.CAMCORDER);
this.mediarecorder.setVideoSource(android.media.MediaRecorder.VideoSource.CAMERA);
//this.mediarecorder.setOutputFormat(android.media.MediaRecorder.OutputFormat.MPEG_4);
// Step 3: Set a CamcorderProfile (requires API Level 8 or higher)
this.mediarecorder.setProfile(android.media.CamcorderProfile.get(android.media.CamcorderProfile.QUALITY_HIGH));
// platform.screen.mainScreen.widthDIPs
// platform.screen.mainScreen.heightDIPs
// Step 4: Set output file
var fileName = "videoCapture_" + new Date() + ".mp4";
var path = android.os.Environment.getExternalStoragePublicDirectory(android.os.Environment.DIRECTORY_DCIM).getAbsolutePath() + "/Camera/" + fileName;
this.file = new java.io.File(path);
this.mediarecorder.setOutputFile(this.file.toString());
this.mediarecorder.setOrientationHint(270);
try {
this.mediarecorder.prepare();
this.mediarecorder.start();
} catch( ex ) {
console.log(ex);
}
Then, the image suddenly becomes darker, and my face (its what's in focus when I'm trying it out) gets wider. So the aspect ratio changes, and so does the lighting somehow.
I have tried setting setPictureSize on the camera parameters, and setVideoSize on the MediaRecorder with no luck. And for the lighting change, I have simply no clue as to whats going on. Now I've been googling myself half way to heaven, and still found nothing, so I hope someone here has got any tip on what to pursue next?
Video recording generally tries to run at a steady frame rate, such as 30fps. Camera preview often slows itself down to 10-15fps to maintain brightness, so if you're in a darker location, video recording will be darker (since it can't expose for longer than 1/30s instead of 1/10s that camera preview can).
Did you call setVideoSize before or after calling setProfile? The setProfile call changes many parameters, including preview size; most video recording sizes are 16:9, and the default camera preview resolution is likely a 4:3 size. So when you start the recording, the aspect ratio switches.
Most video recording apps use 16:9 preview sizes even before starting recording so that they're consistent. You can also record 4:3 video, but that's generally not what people want to see.
A week ago I've started researching the Android camera API. I have successfully inited the camera and started preview, and it worked fine. Then I've found out I wasn't initializing and releasing the camera properly, so I overhauled the code somewhat, and now I have a problem that didn't occur initially: extremely low FPS. About 0.5, that's 2 seconds per frame. Interestingly enough, I get 1 frame with a delay, and then a second frame immediately after (1-15 ms), followed again by 2 seconds delay before the next frame.
This is my camera initialization code:
m_openedCamera = Camera.open(id);
m_surfaceHolder = new SurfaceView(MyApplication.instance().getApplicationContext()).getHolder();
Assert.assertNotNull(m_openedCamera);
// This is required on A500 for some reason
Camera.Parameters params = m_openedCamera.getParameters();
params.setPreviewFormat(ImageFormat.NV21);
params.setPreviewSize(320, 240);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH)
{
params.setRecordingHint(true);
params.setAutoExposureLock(true);
params.setAutoWhiteBalanceLock(true);
}
m_openedCamera.setParameters(params);
int bitsPerPx = ImageFormat.getBitsPerPixel(ImageFormat.NV21);
int width = params.getPreviewSize().width;
int height = params.getPreviewSize().height;
int size = (int)(width * height * bitsPerPx / 8.0);
m_openedCamera.addCallbackBuffer( new byte[size] );
m_openedCamera.addCallbackBuffer( new byte[size] );
m_openedCamera.addCallbackBuffer( new byte[size] );
m_openedCamera.addCallbackBuffer( new byte[size] );
m_openedCamera.setErrorCallback(this);
m_openedCamera.setPreviewDisplay(m_surfaceHolder);
m_openedCameraFacing = facing;
m_openedCamera.setPreviewCallback(this);
m_openedCamera.startPreview();
I have just added callback buffers - hasn't changed anything. In my initial code from a week ago I had no surface view, but removing it now has no effect either.
This occurs on my second, much newer tablet as well, even though FPS is higher there (8-10), and there's no double frame there, the frames are spaced evenly. The FPS used to be at least 20. Light conditions haven't changed between now and then, btw.
Update: tried opening the camera in a separate thread as described here - no change.
params.setRecordingHint(true); is used to start video record starts.
Please check it again with "params.setRecordingHint(false)"
Please make your own previewcallbak function and then
private PreviewCallback mPreviewCallback = new PreviewCallback();
....
setPreviewCallback(mPreviewCallback);
Regarding PreviewCallback(), you can refer to CameraTest.Java in cts directory : cts/tests/tests/hawdware/src/android/hardware/cts
From your comment below
"I have successfully inited the camera and started preview, and it worked fine. Then I've found out I wasn't initializing and releasing the camera properly"
I guess you was using the default camera parameters defined in camera HAL at first. Although it is not clear what "initializing" means, I think they is likely camera parameters setting.
So, I'd like to suggest you remove the code for params and then test it again.
Or, you can test it one by one
Remove only "params.setPreviewSize(320, 240);"
Remove only "params.setPreviewFormat(ImageFormat.NV21);"
Remove both 1 and 2.
I have tried to get the supported video size as below in an emulator but it always return null.Why it is so?I have tried in 4.03.Thanks in advance
Camera camera=Camera.open();
android.hardware.Camera.Parameters params = camera.getParameters();
supportedPicSizes = params.getSupportedVideoSizes();
if (supportedPicSizes==null){
Log.i("*****supportedVideoSize*****", "*****Null****");
}
This is a known Android bug.
This still hasn't been fixed yet but the fact that it is on the bug tracking system probably means that Google has plans to see it through.
Here is the one of the option by which you can get Camera Preview Size of the devices.
Camera camera=Camera.open();
android.hardware.Camera.Parameters params = camera.getParameters();
Size supportedPicSizes = params.getPreviewSize();
if (supportedPicSizes==null){
Log.i("*****supportedVideoSize*****", "*****Null****");
}
else{
Log.i("*****supportedVideoSize*****", "*****"+supportedPicSizes.height);
Log.i("*****supportedVideoSize*****", "*****"+supportedPicSizes.width);
}
Hope it helps you.
Thanks.
It's clearly stated here that a null return from this method means the device doesn't support different outputs for preview and video. In case of emulator, this situation must be prominent because an emulator doesn't have a physical camera and is generally not used for testing camera-related modules.
I would like to add that even though documentations have pointed this to be a normal case scenario, I'm still unable to find a proper alternative for devices suffering from this sickness. For instance, a Verizon variant of S3 return null for both "getSupportedVideoSizes()" and "getPreferredPreviewSizeForVideo()". Has anyone gone through a way around this issue?? A help would be highly appreciated.
answered also here
Sample code:
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();
}
}
I've tried several answer I've found across the web, such as:
Camera.Parameters parameters = mCamera.getParameters();
parameters.set("camera-id", 2);
mCamera.setParameters(parameters);
or
mMediaRecorder.setVideoSource(2);
But it doesn't work. I've also set permissions on the manifest file:
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
Am i missing out on something? I've searched StackOverflow and I know this has been asked before but there seem to be no confirmed solution on this, any kind of help would be appreciated.
Note: I'm using Galaxy S on the 2.1 platform
Anyway after a few trials and error, I figured it out how to do it:
Camera.Parameters parameters = mCamera.getParameters();
parameters.set("camera-id", 2);
parameters.setPreviewSize(640, 480); // or (800,480) this is supported front camera preview size # Samsung Galaxy S
mCamera.setParameters(parameters);
Or, if you need to use it with MediaRecorder:
MediaRecorder mMediaRecorder = new MediaRecorder();
Camera mCamera = Camere.open();
Camera.Parameters parameters = mCamera.getParameters();
parameters.set("camera-id", 2);
parameters.setPreviewSize(640, 480); // or (800,480)
mCamera.setParameters(parameters);
mCamera.unlock(); // unlock, to give other process to access it otherwise it can't be used later
mMediaRecorder.setCamera(mCamera);
// continue with mMediaRecorder standard routines here
If you need to have a smaller preview size, you could set/scale down your SurfaceView size instead.
There currently isn't a standard API for front-facing cameras; you will need to rely on whatever (if any) documentation the hardware manufacturer has for using their extensions to access the front-facing camera. This will of course only work on those specific devices.
Note writing raw strings like "camera-id" is often a good sign you have wandered into the woods outside of the official SDK. :)
I think this is as good a place as any to add some details I've worked out.
In case you're using the front facing camera in portrait mode and the resulting file comes out cut up into squares with green blocks thrown in try inverting the width and height (both preview and recorder) and setting the encoder to H263...
This solved the problem on my Samsung Galaxy S on 2.3.3+...
Can android MediaRecorder capture video with resolution higher than 320*240?
When I used MediaRecorder::setVideoSize() to set the video size, the captured video were all at the resolution of 320*240. Whats even worse, the higher ones can not get a clear video, they were somehow greenish. (encoder used is h263, format is mpeg4)
Android version used here is 1.6
Could you please anyone help me out?
I had an issue similar to what is described here. I turned out that I had to restructure my code slightly before I could adjust the video size.
The important thing is that setVideoSize() is called before setVideoEncoder(). I can't find this in the documentation, however it solved my problems with setting a specific video resolution.
Also keep in mind that setOutputFormat() should be called before setVideoSize().
As as side note the same is true for setVideoFrameRate(). If it is not called before setVideoEncoder() it will not have any affect.
This was tested with Android 2.3.3
Here is a code example:
recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setVideoSize(640,480);
recorder.setVideoFrameRate(30);
recorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
First you are going to want to determine what your Camera supports. Try:
List<String> anti = params.getSupportedAntibanding();
List<String> color = params.getSupportedColorEffects();
List<String> focus = params.getSupportedFocusModes();
List<String> flash = params.getSupportedFlashModes();
List<Size> previewSize = params.getSupportedPreviewSizes();
List<Size> sizes = params.getSupportedPictureSizes();
List<Integer> frameRates = params.getSupportedPreviewFrameRates();
List<Integer> pictureFormats = params.getSupportedPictureFormats();
List<String> scene = params.getSupportedSceneModes();
List<String> white = params.getSupportedWhiteBalance();
This will tell you all of the Camera's supported parameters. Second Make sure that you initialize your MediaRecorder properly see the google documentation for the order in which you need to set the MediaRecorder. Also, if your Camera.setPreviewSize and MediaRecorder.setVideoSize are different it can cause odd behavior.