audio and video capture of an activity from kitkat version support - android

Iam developing an android application "User album" in which user pics are scrolling in a viewpager with a background music,here if user wants to make a video with scrolling pics and audio music we should provide a "video screen cast option" to make a video.
I write some code like this to make a video cast.
My code:
public void onToggleScreenShare(View view) {
if (((ToggleButton) view).isChecked()) {
initRecorder();
shareScreen();
} else {
mMediaRecorder.stop();
mMediaRecorder.reset();
Log.v(TAG, "Stopping Recording");
stopScreenSharing();
}
}
private void shareScreen() {
if (mMediaProjection == null) {
startActivityForResult(mProjectionManager.createScreenCaptureIntent(), REQUEST_CODE);
return;
}
mVirtualDisplay = createVirtualDisplay();
mMediaRecorder.start();
}
private VirtualDisplay createVirtualDisplay() {
return mMediaProjection.createVirtualDisplay("MainActivity",
DISPLAY_WIDTH, DISPLAY_HEIGHT, mScreenDensity,
DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,
mMediaRecorder.getSurface(), null /*Callbacks*/, null
/*Handler*/);
}
private void initRecorder() {
try {
/*****here mMediaRecorder is not supported for kitkat version*******/
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mMediaRecorder.setOutputFile(Environment
.getExternalStoragePublicDirectory(Environment
.DIRECTORY_DOWNLOADS) + "/video_"+System.currentTimeMillis()+".mp4");
mMediaRecorder.setVideoSize(DISPLAY_WIDTH, DISPLAY_HEIGHT);
mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mMediaRecorder.setVideoEncodingBitRate(512 * 1000);
mMediaRecorder.setVideoFrameRate(30);
int rotation = getWindowManager().getDefaultDisplay().getRotation();
int orientation = ORIENTATIONS.get(rotation + 90);
mMediaRecorder.setOrientationHint(orientation);
mMediaRecorder.prepare();
} catch (IOException e) {
e.printStackTrace();
}
}
private class MediaProjectionCallback extends MediaProjection.Callback {
#Override
public void onStop() {
if (mToggleButton.isChecked()) {
mToggleButton.setChecked(false);
mMediaRecorder.stop();
mMediaRecorder.reset();
Log.v(TAG, "Recording Stopped");
}
mMediaProjection = null;
stopScreenSharing();
}
}
private void stopScreenSharing() {
if (mVirtualDisplay == null) {
return;
}
mVirtualDisplay.release();
//mMediaRecorder.release(); //If used: mMediaRecorder object cannot
// be reused again
destroyMediaProjection();
}
By using th eabove code it works well from lollipop but FOR KITKAT VERSION it is saying that media recorder is not supported for kitkat version, i tried for other codes but didn't find the correct solution.
can you suggest me how to implement video cast for KitKat OS version?

Related

android camera2 release resources doesn't work correctly

I want to create a custom camera app in android. I have one activity that uses two fragments. One fragment is for camera and another is for show picture or video that was taken. When activity starts, It loads camera fragment. In preview of camera there is a button that uses for record video. In long press on button recording starts and after end of long press video recording stops and file is send to the fragment that show the video. In other word, at the end of long press on button I replace the camera fragment with a new fragment that show the result. In onPause of camera fragment I stopped the cameradevice but after some back and forth between fragments I can't use the camera at all. After some back and forth no camera application doesn't work in my phone and I have to restart my phone.
these are some codes of my camera fragment
#OnLongClick(R.id.capture)
public boolean captureLongPressed() {
startRecord();
mMediaRecorder.start();
return true;
}
#OnTouch(R.id.capture)
public boolean captureOnTouch(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
if (isRecordButtonLongPressed) {
mIsRecording = false;
try {
mMediaRecorder.stop();
mMediaRecorder.reset();
} catch (RuntimeException ex) {
}
if (listener != null)
listener.onVideoRecorded(mVideoFileName);
return true;
}
}
return false;
}
private void startRecord() {
try {
setupMediaRecorder();
SurfaceTexture surfaceTexture = mTextureView.getSurfaceTexture();
surfaceTexture.setDefaultBufferSize(mPreviewSize.getWidth(), mPreviewSize.getHeight());
Surface previewSurface = new Surface(surfaceTexture);
Surface recordSurface = mMediaRecorder.getSurface();
mCaptureRequestBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_RECORD);
mCaptureRequestBuilder.addTarget(previewSurface);
mCaptureRequestBuilder.addTarget(recordSurface);
mCameraDevice.createCaptureSession(Arrays.asList(previewSurface, recordSurface), new CameraCaptureSession.StateCallback() {
#Override
public void onConfigured(#NonNull CameraCaptureSession session) {
try {
session.setRepeatingRequest(mCaptureRequestBuilder.build(), null, null);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
#Override
public void onConfigureFailed(#NonNull CameraCaptureSession session) {
}
}, null);
} catch (Exception e) {
e.printStackTrace();
}
}
private void setupMediaRecorder() throws IOException {
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mMediaRecorder.setOutputFile(mVideoFileName);
mMediaRecorder.setVideoEncodingBitRate(1000000);
mMediaRecorder.setVideoFrameRate(30);
mMediaRecorder.setVideoSize(mVideoSize.getWidth(), mVideoSize.getHeight());
mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
mMediaRecorder.setOrientationHint(mTotalRotation);
mMediaRecorder.prepare();
}
#Override
public void onPause() {
closeCamera();
stopBackgroundThread();
super.onPause();
}
private void closeCamera() {
if (null != mCameraDevice) {
mCameraDevice.close();
mCameraDevice = null;
}
}

Screen recording in android using MediaRecorder api

Hey I'm trying to record a screencast app in android lolipop using mediarecorder api. Problem is that my application acts weird. Whenever I call start recording on the VideoRecorder class even when hardcoding the configuration like video size and output file the phone reboots. The app previosuly worked fine - saved the stuff in the correct place and the video itself looked good but then I've changed something in the code and now it doesn't work. Any idea what I'm missing?
Here is my code:
public class VideoRecorder {
private int screenDensity, screenHeight, screenWidth;
private MediaRecorder mMediaRecorder;
private VirtualDisplay mVirtualDisplay;
private MediaProjection mediaProjection;
private String directory, filename;
private Display defaultDisplay = null;
private DisplayMetrics metrics;
public VideoRecorder(Display defaultDisplay) {
//this.defaultDisplay = defaultDisplay;
//metrics = new DisplayMetrics();
//defaultDisplay.getMetrics(metrics);
//screenDensity = metrics.densityDpi;
// screenHeight = metrics.heightPixels;
// screenWidth = metrics.widthPixels;
}
void prepareVideoRecorder() {
initRecorder();
prepareRecorder();
}
private void initRecorder() {
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mMediaRecorder.setVideoEncodingBitRate(512 * 1000);
mMediaRecorder.setVideoFrameRate(30);
mMediaRecorder.setVideoSize(540, 888);
// mMediaRecorder.setOutputFile(directory + "/" + filename + ".mp4");
mMediaRecorder.setOutputFile(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES).getAbsoluteFile() + "/recorderrsr.mp4");
}
void startRecording(MediaProjection mediaProjection) {
mMediaRecorder = new MediaRecorder();
initRecorder();
prepareRecorder();
this.mediaProjection = mediaProjection;
mMediaRecorder.start();
mVirtualDisplay = createVirtualDisplay();
}
private VirtualDisplay createVirtualDisplay() {
return mediaProjection.createVirtualDisplay("MainActivity",
540, 888, 240,
DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,
mMediaRecorder.getSurface(), null /*Callbacks*/, null /*Handler*/);
}
void setFilename(String filename) {
this.filename = filename;
}
void setDirectory(String directory) {
this.directory = directory;
}
void stopRecording() {
mMediaRecorder.stop();
mMediaRecorder.reset();
if (mediaProjection != null) {
mediaProjection.stop();
mediaProjection = null;
}
if (mMediaRecorder != null) {
mMediaRecorder.release();
mMediaRecorder = null;
}
if (mVirtualDisplay != null) {
mVirtualDisplay.release();
}
}
private void prepareRecorder() {
try {
mMediaRecorder.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
void release() {
mMediaRecorder.release();
}
}
I'm sure that the location exists and I have the correct permissions - as I've said the app worked fine before.
Thanks for help Jon

Can I test screen record in emulator in Android Studio?

I try to test a sample code about screen record from below link, I modified some code to disable recording audio.
http://www.truiton.com/2015/05/capture-record-android-screen-using-mediaprojection-apis/
I test the code in Android Studio V1.3, but I get the following error, and the file capture.mp4 is blank.
I'm not sure whether I must test the code in real mobile phone? Could you help me ? Thanks!
Error Info
09-22 06:41:50.250 2167-2167/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: screencapture.truiton.com.myapplication, PID: 2167
java.lang.RuntimeException: stop failed.
at android.media.MediaRecorder.stop(Native Method)
at screencapture.truiton.com.myapplication.MainActivity.onToggleScreenShare(MainActivity.java:93)
at screencapture.truiton.com.myapplication.MainActivity$1.onClick(MainActivity.java:55)
at android.view.View.performClick(View.java:4780)
at android.widget.CompoundButton.performClick(CompoundButton.java:120)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Source Code
package screencapture.truiton.com.myapplication;
public class MainActivity extends Activity {
private static final String TAG = "MainActivity";
private static final int PERMISSION_CODE = 1;
private int mScreenDensity;
private MediaProjectionManager mProjectionManager;
private static final int DISPLAY_WIDTH = 480;
private static final int DISPLAY_HEIGHT = 640;
private MediaProjection mMediaProjection;
private VirtualDisplay mVirtualDisplay;
private MediaProjectionCallback mMediaProjectionCallback;
private ToggleButton mToggleButton;
private MediaRecorder mMediaRecorder;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
mScreenDensity = metrics.densityDpi;
mMediaRecorder = new MediaRecorder();
initRecorder();
prepareRecorder();
mProjectionManager = (MediaProjectionManager) getSystemService
(Context.MEDIA_PROJECTION_SERVICE);
mToggleButton = (ToggleButton) findViewById(R.id.toggle);
mToggleButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onToggleScreenShare(v);
}
});
mMediaProjectionCallback = new MediaProjectionCallback();
}
#Override
public void onDestroy() {
super.onDestroy();
if (mMediaProjection != null) {
mMediaProjection.stop();
mMediaProjection = null;
}
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode != PERMISSION_CODE) {
Log.e(TAG, "Unknown request code: " + requestCode);
return;
}
if (resultCode != RESULT_OK) {
Toast.makeText(this,
"Screen Cast Permission Denied", Toast.LENGTH_SHORT).show();
mToggleButton.setChecked(false);
return;
}
mMediaProjection = mProjectionManager.getMediaProjection(resultCode, data);
mMediaProjection.registerCallback(mMediaProjectionCallback, null);
mVirtualDisplay = createVirtualDisplay();
mMediaRecorder.start();
}
public void onToggleScreenShare(View view) {
if (((ToggleButton) view).isChecked()) {
shareScreen();
} else {
mMediaRecorder.stop();
mMediaRecorder.reset();
Log.v(TAG, "Recording Stopped");
stopScreenSharing();
initRecorder();
prepareRecorder();
}
}
private void shareScreen() {
if (mMediaProjection == null) {
startActivityForResult(mProjectionManager.createScreenCaptureIntent(), PERMISSION_CODE);
return;
}
mVirtualDisplay = createVirtualDisplay();
mMediaRecorder.start();
}
private void stopScreenSharing() {
if (mVirtualDisplay == null) {
return;
}
mVirtualDisplay.release();
//mMediaRecorder.release();
}
private VirtualDisplay createVirtualDisplay() {
return mMediaProjection.createVirtualDisplay("MainActivity",
DISPLAY_WIDTH, DISPLAY_HEIGHT, mScreenDensity,
DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,
mMediaRecorder.getSurface(), null /*Callbacks*/, null /*Handler*/);
}
private class MediaProjectionCallback extends MediaProjection.Callback {
#Override
public void onStop() {
if (mToggleButton.isChecked()) {
mToggleButton.setChecked(false);
mMediaRecorder.stop();
mMediaRecorder.reset();
Log.v(TAG, "Recording Stopped");
initRecorder();
prepareRecorder();
}
mMediaProjection = null;
stopScreenSharing();
Log.i(TAG, "MediaProjection Stopped");
}
}
private void prepareRecorder() {
try {
mMediaRecorder.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
finish();
} catch (IOException e) {
e.printStackTrace();
finish();
}
}
private void initRecorder() {
//mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
//mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mMediaRecorder.setVideoEncodingBitRate(512 * 1000);
mMediaRecorder.setVideoFrameRate(30);
mMediaRecorder.setVideoSize(DISPLAY_WIDTH, DISPLAY_HEIGHT);
mMediaRecorder.setOutputFile(Environment.getExternalStorageDirectory() + "/capture.mp4");
}
}
From my experience, Android emulators do allow reading from surfaces (which is the only thing really required for recording) but fail to encode video (and sometimes take screenshots) due to bugs/limitations in framework Java code. There are two primary reasons:
The emulator may not declare support for EGL_ANDROID_recordable extension, which is demanded by MediaRecorder/MediaCodec. You can check for the extension support by executing following command:
adb shell dumpsys SurfaceFlinger | grep EGL_ANDROID_recordable
The only color format, supported by Android 6 builtin software encoder, is YUV. Video buffers, coming from emulator GPU, are likely to have different format.
Note, that above limitations aren't exclusive to emulator and may cause failures on actual devices as well. You can work around them by doing format conversion in software (on CPU or via GLES), using approach outlines in this answer, but the performance of software encoder is going to be abysmal either way.

How to use Mediaprojection library in android to capture screen and convert into mp4 file?

Since android 5.0 they are providing mediaprojection library to capture screen content. but sample demo application provided by them is not clear. U can find sample app here. In that application they are projecting captured screen using virtualdisplay method
private void setUpVirtualDisplay() {
Log.i(TAG, "Setting up a VirtualDisplay: " +
mSurfaceView.getWidth() + "x" + mSurfaceView.getHeight() +
" (" + mScreenDensity + ")");
mVirtualDisplay = mMediaProjection.createVirtualDisplay("ScreenCapture",
mSurfaceView.getWidth(), mSurfaceView.getHeight(), mScreenDensity,
DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,
mSurface, null, null);
mButtonToggle.setText(R.string.stop);
}
I want to convert captured screen into mp4 file for my screen recording application. Please help me to get through this.
here is sample code refference from
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private static final int PERMISSION_CODE = 1;
private int mScreenDensity;
private MediaProjectionManager mProjectionManager;
private static final int DISPLAY_WIDTH = 480;
private static final int DISPLAY_HEIGHT = 640;
private MediaProjection mMediaProjection;
private VirtualDisplay mVirtualDisplay;
private MediaProjectionCallback mMediaProjectionCallback;
private ToggleButton mToggleButton;
private MediaRecorder mMediaRecorder;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
mScreenDensity = metrics.densityDpi;
mMediaRecorder = new MediaRecorder();
initRecorder();
prepareRecorder();
mProjectionManager = (MediaProjectionManager) getSystemService
(Context.MEDIA_PROJECTION_SERVICE);
mToggleButton = (ToggleButton) findViewById(R.id.toggle);
mToggleButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onToggleScreenShare(v);
}
});
mMediaProjectionCallback = new MediaProjectionCallback();
}
#Override
public void onDestroy() {
super.onDestroy();
if (mMediaProjection != null) {
mMediaProjection.stop();
mMediaProjection = null;
}
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode != PERMISSION_CODE) {
Log.e(TAG, "Unknown request code: " + requestCode);
return;
}
if (resultCode != RESULT_OK) {
Toast.makeText(this,
"Screen Cast Permission Denied", Toast.LENGTH_SHORT).show();
mToggleButton.setChecked(false);
return;
}
mMediaProjection = mProjectionManager.getMediaProjection(resultCode, data);
mMediaProjection.registerCallback(mMediaProjectionCallback, null);
mVirtualDisplay = createVirtualDisplay();
mMediaRecorder.start();
}
public void onToggleScreenShare(View view) {
if (((ToggleButton) view).isChecked()) {
shareScreen();
} else {
mMediaRecorder.stop();
mMediaRecorder.reset();
Log.v(TAG, "Recording Stopped");
stopScreenSharing();
initRecorder();
prepareRecorder();
}
}
private void shareScreen() {
if (mMediaProjection == null) {
startActivityForResult(mProjectionManager.createScreenCaptureIntent(), PERMISSION_CODE);
return;
}
mVirtualDisplay = createVirtualDisplay();
mMediaRecorder.start();
}
private void stopScreenSharing() {
if (mVirtualDisplay == null) {
return;
}
mVirtualDisplay.release();
//mMediaRecorder.release();
}
private VirtualDisplay createVirtualDisplay() {
return mMediaProjection.createVirtualDisplay("MainActivity",
DISPLAY_WIDTH, DISPLAY_HEIGHT, mScreenDensity,
DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,
mMediaRecorder.getSurface(), null /*Callbacks*/, null /*Handler*/);
}
private class MediaProjectionCallback extends MediaProjection.Callback {
#Override
public void onStop() {
if (mToggleButton.isChecked()) {
mToggleButton.setChecked(false);
mMediaRecorder.stop();
mMediaRecorder.reset();
Log.v(TAG, "Recording Stopped");
initRecorder();
prepareRecorder();
}
mMediaProjection = null;
stopScreenSharing();
Log.i(TAG, "MediaProjection Stopped");
}
}
private void prepareRecorder() {
try {
mMediaRecorder.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
finish();
} catch (IOException e) {
e.printStackTrace();
finish();
}
}
private void initRecorder() {
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mMediaRecorder.setVideoEncodingBitRate(512 * 1000);
mMediaRecorder.setVideoFrameRate(30);
mMediaRecorder.setVideoSize(DISPLAY_WIDTH, DISPLAY_HEIGHT);
mMediaRecorder.setOutputFile("/sdcard/capture.mp4");
}
}
You're passing the Surface from a SurfaceView into createVirtualDisplay(). Replace that with the Surface from a MediaRecorder.
Look at this POST. There is a good explanation on how to use MediaProjection API to actually record the screen to an mp4 file on the external storage. This solution uses the MediaRecorder to store the video.
You can find another solution on the page of Matt Snider. There the MediaMuxer is used to store the video on to the external storage. But note that the output of the MediaMuxer is not streamable.

Camera Server died - Error 100 persists in some devices

I'm trying to develop a video recorder with the front and back camera. The app can switch between the back (default) and front camera. When the app is activated the front camera, and I press the recording button, the app goes to on error method, and I release and init the camera and the recording, but the error persists another time. Any idea?
UPDATE: I update the onError method, with the code below, and I think I don't make the release and the init camera well, because when the app executes this method the surface holder is black and doesn't give me what the camera sees
Here, my code:
onError method:
public void onError(MediaRecorder mr, int what, int extra) {
stopRecording();
//if Error 100, app must release the Camera object and instantiate a new one.
if (what == 100) {
if (error_100 == 2) { //Error 100 persists, change camera
if (Camera.getNumberOfCameras() <= 1) {
//No mas camaras para app
} else {
if (selected_camera == FRONT_CAMERA) {
selected_camera=BACK_CAMERA;
} else {
selected_camera=FRONT_CAMERA;
}
selected_camera_button.setEnabled(false);
Toast.makeText(this, "Initializing other camera", Toast.LENGTH_SHORT).show();
}
} else { //No camera init finish activity
if (error_100 == 3) {
Toast.makeText(this, "Initialize cameras failed", Toast.LENGTH_SHORT).show();
finish();
} else {
Toast.makeText(this, "Recording error has occurred. Stopping the recording", Toast.LENGTH_SHORT).show();
}
}
error_100++;
initCamera();
initCameraRecorder();
}
}
And the other method:
/** Initializes the back/front camera */
private boolean initCamera() {
try {
camera = getCameraInstance(selected_camera);
Camera.Parameters camParams = camera.getParameters();
camParams.set( "cam_mode", 1 );
camParams.set("orientation", "portrait");
checkCameraFlash(camParams);
//Establish the rotation angle camera
setAngleCameraRotation();
camera.setDisplayOrientation(angle_rotation_camera);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
Establish 4:3 ratio and 480x640 if is posible
setAspectResolutionCamera(camParams);
camera.setParameters(camParams);
//Camera eye
video_view.getHolder().setFixedSize(height_video, width_video);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(height_video, width_video);
video_view.setLayoutParams(lp);
camera.lock();
surface_holder = video_view.getHolder();
surface_holder.addCallback(this);
surface_holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
setPreviewCamera();
} catch(Exception e) {
Log.v("RecordVideo", "Could not initialize the Camera");
return false;
}
return true;
}
/** Initializes the camera recorder before starts the recording */
private void initCameraRecorder() {
if(media_recorder != null) return;
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
output_file_name = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) + File.separator + timeStamp + ".mp4";
File outFile = new File(output_file_name);
if(outFile.exists()) {
outFile.delete();
}
try {
camera.stopPreview();
camera.unlock();
media_recorder = new MediaRecorder();
media_recorder.setCamera(camera);
media_recorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
media_recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
CamcorderProfile profile = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
profile.videoBitRate = 885000; //Medium quality
profile.videoFrameHeight = height_video;
profile.videoFrameWidth = width_video;
media_recorder.setProfile(profile);
media_recorder.setMaxDuration(21000); // limit to 21 seconds
media_recorder.setOrientationHint(setOrientationCameraRecorder());
media_recorder.setPreviewDisplay(surface_holder.getSurface());
media_recorder.setOutputFile(output_file_name);
media_recorder.prepare();
Log.v("RecordVideo", "MediaRecorder initialized");
} catch(Exception e) {
Log.v("RecordVideo", "MediaRecorder failed to initialize");
e.printStackTrace();
}
}
/** Starts the recording video */
private void beginRecording() {
media_recorder.setOnInfoListener(this);
media_recorder.setOnErrorListener(this);
media_recorder.start();
record_button.setTextColor(getResources().getColor(android.R.color.holo_red_dark));
}
/** Stops the recording video */
private void stopRecording() {
if (media_recorder != null) {
media_recorder.setOnErrorListener(null);
media_recorder.setOnInfoListener(null);
try {
media_recorder.stop();
} catch(IllegalStateException e) {
// This can happen if the recorder has already stopped.
Log.e("INFO:", "Got IllegalStateException in stopRecording");
}
releaseRecorder();
record_button.setTextColor(getResources().getColor(android.R.color.black));
releaseCamera();
}
video_task.cancel(true);
output_file_name.isEmpty();
}

Categories

Resources