I'm trying to develop a simple video recorder
I initialize Camera, and then on button click I run a Runnable that prepares the MediaRecorder:
public void run() {
mCamera.unlock();
mMediaRecorder = new MediaRecorder();
mMediaRecorder.setCamera(mCamera);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mMediaRecorder.setVideoSize(320, 240);
mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
mMediaRecorder.setOutputFile(mSomeFile);
mMediaRecorder.setMaxDuration(MAX_DURATION);
mMediaRecorder.setOrientationHint(90);
try {
mMediaRecorder.prepare();
mMediaRecorder.start();
} catch (Exception e) {
e.printStackTrace();
}
}
Every thing runs fine but the actual output video contains some blank frames at the beginning. Could it be a hardware issue or rather it needs some optimization from the code?
Related
I am having a basic Camera application using MediaRecorder. Application previews the video on TextureView and when button is clicked it starts recording. I works on most mobile phones, but on some specific device, underlying OMX fails:
(40b1df38) hardware/ti/omap4xxx/camera/OMXCameraAdapter/OMXCameraAdapter.cpp:2176 UseBuffersPreview - Exiting function UseBuffersPreview because of ret 0 eError=80001018
and in consequence, preview freezes and recording fails.
I found that this is most likely caused when Camera object is still previewing, while MediaRecorder object start recording. When I call mCamera.stopPreview() before starting recording, it works. Therefore, I have following questions:
1. Why is call to mCamera.stopPreview() necessary on some devices, and not on others?
2. How can the recorded video be still previewed, if stopPreview() on Camera object is called, but MediaRecorder.setPreviewDisplay() was not?
Below I present some code:
public boolean startRecording() throws IllegalStateException, IOException {
if (!mRecordingStarted) {
mMediaRecorder = new MediaRecorder();
try {
mCamera.stopPreview(); // !!! without this recording fails on some devices!
mCamera.unlock();
mMediaRecorder.setCamera(mCamera);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT); mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mMediaRecorder.setProfile(mCamProfile);
//mMediaRecorder.setPreviewDisplay(mSurfaceView.getHolder().getSurface());
mMediaRecorder.setOutputFile(mCaptureFile);
mMediaRecorder.prepare();
mMediaRecorder.start();
mRecordingStarted = true;
} catch (Exception e) {
mMediaRecorder.release();
mCamera.lock();
Log.e("ERROR", e.toString());
}
}
return mRecordingStarted;
}
private void startPreview() throws IllegalStateException, IOException {
if (mCamera == null) {
return;
}
try {
mCamera.stopPreview();
} catch (Exception e) {
}
Camera.Parameters parameters = mCamera.getParameters();
mCamera.setDisplayOrientation(0);
mCamProfile = getConfiguredProfile();
mVideoSize = getBestPreviewSize(mCamProfile, parameters);
parameters.setPreviewSize(mVideoSize.width, mVideoSize.height);
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_INFINITY);
mCamera.setParameters(parameters);
mCamera.setPreviewTexture(mPreview.getSurfaceTexture());
mCamera.startPreview();
}
Has anyone managed to successfully record a video using MediaRecorder on Glass?
This is the code i am using in order to prepare the Recorder. I keep getting error -19.
recorder = new MediaRecorder();
recorder.setOutputFile(videoFile);
recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setVideoFrameRate(15);
recorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
Thank you.
Update:
It seems to be a GDK bug. I have reported it and it got accepted. If you are having the same issue just star the bug report to stay informed:
https://code.google.com/p/google-glass-api/issues/detail?id=360
To start video capture on Glass it seems that you have to completely stop the video preview. If you are using a previewing camera, prior of doing anything with a MediaRecorder, just run:
try {
mCamera.setPreviewDisplay(null);
} catch (java.io.IOException ioe) {
Log.d(TAG, "IOException nullifying preview display: " + ioe.getMessage());
}
mCamera.stopPreview();
mCamera.unlock();
More info here: https://code.google.com/p/google-glass-api/issues/detail?id=360#c6
After much frustration and iteration, I'm pleased to report that it IS possible to use MediaRecorder on Glass XE12. The below code works for me on my Glass Version 1 running XE12:
mMediaRecorder = new MediaRecorder();
mMediaRecorder.setCamera(mCamera);
mMediaRecorder.setPreviewDisplay(preview.getHolder().getSurface());
mCamera.unlock();
// Step 2: Set sources
mMediaRecorder.setOnErrorListener(new android.media.MediaRecorder.OnErrorListener() {
public void onError(MediaRecorder mediarecorder1, int k, int i1)
{
Log.e(TAG,String.format("Media Recorder error: k=%d, i1=%d", k, i1));
}
});
mMediaRecorder.setVideoSource(0);
mMediaRecorder.setAudioSource(0);
mMediaRecorder.setOutputFormat(2);
mMediaRecorder.setVideoEncoder(2);
mMediaRecorder.setVideoEncodingBitRate(0x4c4b40);
mMediaRecorder.setVideoFrameRate(30);
mMediaRecorder.setVideoSize(1280, 720);
mMediaRecorder.setAudioChannels(2);
mMediaRecorder.setAudioEncoder(3);
mMediaRecorder.setAudioEncodingBitRate(0x17700);
mMediaRecorder.setAudioSamplingRate(44100);
mMediaRecorder.setMaxDuration(0);
mMediaRecorder.setOutputFile(getOutputMediaFile(MEDIA_TYPE_VIDEO).toString());
mMediaRecorder.setMaxDuration(-1);
// Step 5: Set the preview output
// Step 6: Prepare configured MediaRecorder
try {
mMediaRecorder.prepare();
mMediaRecorder.start();
} catch (IllegalStateException e) {
Log.d(TAG, "IllegalStateException preparing MediaRecorder: " + e.getMessage());
releaseMediaRecorder();
return false;
} catch (IOException e) {
Log.d(TAG, "IOException preparing MediaRecorder: " + e.getMessage());
releaseMediaRecorder();
return false;
} catch (Exception e) {
Log.d(TAG, "Unknown exception preparing MediaRecorder: " + e.getMessage());
releaseMediaRecorder();
return false;
}
When do you prepare the recorder, is it something that happens when you launch your application? If so, are you launching it with a voice trigger?
If both of those things are true, this might be an instance of the bug described here. Can you try the workaround in that thread (exponential backoff) and see if it works?
I 'm using mediarecorder to capture audio through MIC. I have set the max duration to 20 seconds. The recording stops automatically and does not stop at my break point inside setOnInfoListener.
**UPDATE: Changed my code according to suggestion but still doesnt stop at the breakpoint inside the listener.**
mRecorder.reset();
mRecorder.setOnInfoListener(new OnInfoListener() {
#Override
public void onInfo(MediaRecorder mr, int what, int extra) {
if (what == MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED) {
mRecorder.stop();
}
}
});
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setAudioSamplingRate(8000);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.RAW_AMR);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mRecorder.setOutputFile(fileName);
mRecorder.setMaxDuration(20000);
try {
mRecorder.prepare();
} catch(IOException exception) {
mRecorder.reset();
mRecorder.release();
mRecorder = null;
return;
}
mRecorder.start();
Can someone please tell me why does the code not hit my onInfo method inside the listener rather silently stops recording.
Thanks
When you set the output format, try using THREE_GPP instead of RAW_AMR.
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
According to the documentation for setOutputFormat():
It is recommended to always use 3GP format when using the
H.263 video encoder and AMR audio encoder. Using an MPEG-4
container format may confuse some desktop players.
Try moving your call to setOnInfoListener() before the call to prepare().
In my own video capture code, I invoke setOnInfoListener() right after creating the MediaRecorder object. In your code example, a good place might be right after reset() and before setAudioSource().
Otherwise, the body of your OnInfoListener class looks correct.
I've added the MediaRecorder setup code from my app, which does work correctly.
try {
mCamera.unlock();
mMediaRecorder = new MediaRecorder();
mMediaRecorder.setOnErrorListener( new VideoRecorderErrorListener() );
mMediaRecorder.setOnInfoListener( new VideoRecorderInfoListener() );
// As per Android API docs, the ordering of the following initialization
// calls is important.
mMediaRecorder.setCamera(mCamera);
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
mMediaRecorder.setOutputFile( mOutputFilePath );
mMediaRecorder.setMaxFileSize(VIDEO_MAX_FILE_SIZE);
mMediaRecorder.setAudioChannels(AUDIO_CHANNELS);
mMediaRecorder.setAudioSamplingRate(AUDIO_SAMPLING_RATE);
mMediaRecorder.setAudioEncodingBitRate(AUDIO_ENCODING_BIT_RATE);
mMediaRecorder.setMaxDuration(VIDEO_MAX_DURATION);
mMediaRecorder.setVideoFrameRate(mPictureFPS);
mMediaRecorder.setVideoEncodingBitRate(VIDEO_ENCODING_BIT_RATE);
mMediaRecorder.setVideoSize(mPreviewWidth, mPreviewHeight);
mMediaRecorder.setPreviewDisplay(mHolder.getSurface());
mMediaRecorder.prepare();
mMediaRecorder.start();
} catch (IllegalStateException e) {
as soon as it will reach the maximum duration, it will call the (Customized Method) stopRecording(): Where we can handle all the stop recording and release player camera and preview.
myRecorder = new MediaRecorder();
myRecorder.setOnInfoListener(new MediaRecorder.OnInfoListener() {
#Override
public void onInfo(MediaRecorder mr, int what, int extra) {
if (what == MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED) {
stopRecording();
}
}
});
u passed the MediaRecorder mr object into your method but you didn't use it. Try mr.stop();
I am using the media recorder class for recording video, I initialize the recorder with following properties,
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
But the quality of video is not as same as video I shoot over native android camera, my video recorded using media recorder is of poor quality as compared to the native one, how can I improve the video quality.
If any one knows me help me out.Thanks
Finally I found the code to record high quality video in android 2.1 by setting videEncodingBitRate , AudioEncodingBitRate, AudioSamplingRate ...etc. Using this method you can set the properties for video whatever you want to provide high quality video.
For setting high quality and low quality parameter refer this page,
http://www.andgps.com/20110410/camcorderprofile-predefined-camcorder-profile-settings-for-camcorder-applications
The code i used with base version android 2.1 to produce high quality video is shown below,
recorder = new MediaRecorder();
Method[] methods = recorder.getClass().getMethods();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setVideoFrameRate(24);
recorder.setVideoSize(720, 480);
for (Method method: methods){
try{
if (method.getName().equals("setAudioChannels")){
method.invoke(recorder, String.format("audio-param-number-of-channels=%d", 1));
}
else if(method.getName().equals("setAudioEncodingBitRate")){
method.invoke(recorder,12200);
}
else if(method.getName().equals("setVideoEncodingBitRate")){
method.invoke(recorder, 3000000);
}
else if(method.getName().equals("setAudioSamplingRate")){
method.invoke(recorder,8000);
}
else if(method.getName().equals("setVideoFrameRate")){
method.invoke(recorder,24);
}
}catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
`
use the following settings for Video Recordings:-
private void cameraSettings()
{
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
mediaRecorder.setVideoSize(width, height);
mediaRecorder.setVideoFrameRate(videoFramePerSecond);
}
use videoFramePerSecond = 30 and width = 1280 and height= 720..
This setting you can do by your own as per your requirment.
try this
mrec.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));
mrec.setPreviewDisplay(surfaceHolder.getSurface());
Try adding this line
recorder.setVideoSize(640,480);
Or check out the screen resolutions supported by your device and set the best one accordingly.
To enhance the video quality, you should consider setting the video size on your mediaRecorder instance to the max resolution of your device.
This snippet will do the job
WindowManager wm = (WindowManager) getBaseContext().getSystemService(Context.WINDOW_SERVICE);
Point size = new Point();
wm.getDefaultDisplay().getRealSize(size);
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mMediaRecorder.setOutputFile(file.toString());
mMediaRecorder.setVideoEncodingBitRate(10000000);
mMediaRecorder.setVideoFrameRate(30);
mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.HEVC);
mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
mMediaRecorder.setVideoSize(size.y, size.x);
int rotation =getWindowManager().getDefaultDisplay().getRotation();
switch (mSensorOrientation) {
case SENSOR_ORIENTATION_DEFAULT_DEGREES:
mMediaRecorder.setOrientationHint(DEFAULT_ORIENTATIONS.get(rotation));
break;
case SENSOR_ORIENTATION_INVERSE_DEGREES:
mMediaRecorder.setOrientationHint(INVERSE_ORIENTATIONS.get(rotation));
break;
}
mMediaRecorder.prepare();
I would like to write a program that records video but not sound.
Can anyone help me on how to not record the sound while recording the video using MediaRecorder?
This is indeed possible. See the setAudioEncoder method in MediaRecorder:
If this method is not called, the output file will not contain an audio track.
So if you only want video, simply do not invoke this method.
You should just prepare your mediarecorder's video requirements, like:
private boolean prepareMediaRecorder(){
myCamera = getCameraInstance();
mediaRecorder = new MediaRecorder();
// store the quality profile required
CamcorderProfile profile = CamcorderProfile.get(mCameraId, CamcorderProfile.QUALITY_HIGH);
myCamera.unlock();
mediaRecorder.setCamera(myCamera);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mediaRecorder.setOutputFormat(profile.fileFormat);
mediaRecorder.setVideoEncoder(profile.videoCodec);
mediaRecorder.setVideoEncodingBitRate(profile.videoBitRate);
mediaRecorder.setVideoFrameRate(profile.videoFrameRate);
mediaRecorder.setVideoSize(profile.videoFrameWidth, profile.videoFrameHeight);
mediaRecorder.setOutputFile("/sdcard/myvideo.mp4");
mediaRecorder.setMaxDuration(60000 * 20); // Set max duration 60 *20 sec.
mediaRecorder.setMaxFileSize(5000000 * 4); // Set max file size 5M * 4
mediaRecorder.setPreviewDisplay(myCameraSurfaceView.getHolder().getSurface());
try {
mediaRecorder.prepare();
} catch (IllegalStateException e) {
releaseMediaRecorder();
return false;
} catch (IOException e) {
releaseMediaRecorder();
return false;
}
return true;
}