How can I capture a video recording on Android?
Here is another example which is working
public class EnregistrementVideoStackActivity extends Activity implements SurfaceHolder.Callback {
private SurfaceHolder surfaceHolder;
private SurfaceView surfaceView;
public MediaRecorder mrec = new MediaRecorder();
private Button startRecording = null;
File video;
private Camera mCamera;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.camera_surface);
Log.i(null , "Video starting");
startRecording = (Button)findViewById(R.id.buttonstart);
mCamera = Camera.open();
surfaceView = (SurfaceView) findViewById(R.id.surface_camera);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
menu.add(0, 0, 0, "StartRecording");
menu.add(0, 1, 0, "StopRecording");
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case 0:
try {
startRecording();
} catch (Exception e) {
String message = e.getMessage();
Log.i(null, "Problem Start"+message);
mrec.release();
}
break;
case 1: //GoToAllNotes
mrec.stop();
mrec.release();
mrec = null;
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
protected void startRecording() throws IOException
{
mrec = new MediaRecorder(); // Works well
mCamera.unlock();
mrec.setCamera(mCamera);
mrec.setPreviewDisplay(surfaceHolder.getSurface());
mrec.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mrec.setAudioSource(MediaRecorder.AudioSource.MIC);
mrec.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));
mrec.setPreviewDisplay(surfaceHolder.getSurface());
mrec.setOutputFile("/sdcard/zzzz.3gp");
mrec.prepare();
mrec.start();
}
protected void stopRecording() {
mrec.stop();
mrec.release();
mCamera.release();
}
private void releaseMediaRecorder(){
if (mrec != null) {
mrec.reset(); // clear recorder configuration
mrec.release(); // release the recorder object
mrec = null;
mCamera.lock(); // lock camera for later use
}
}
private void releaseCamera(){
if (mCamera != null){
mCamera.release(); // release the camera for other applications
mCamera = null;
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
if (mCamera != null){
Parameters params = mCamera.getParameters();
mCamera.setParameters(params);
}
else {
Toast.makeText(getApplicationContext(), "Camera not available!", Toast.LENGTH_LONG).show();
finish();
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
mCamera.stopPreview();
mCamera.release();
}
}
camera_surface.xml
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<SurfaceView
android:id="@+id/surface_camera"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
<Button
android:id="@+id/buttonstart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/record_start" />
</RelativeLayout>
And of course include these permission in manifest:
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
The above example will work if you are using rear camera. If you are using front camera, you will have to adjust some things:
First off, you will need to add new permission in the manifest.
<uses-feature android:name="android.hardware.camera.front" android:required="false" />
In your initRecorder
method, instead of
CamcorderProfile cpHigh = CamcorderProfile
.get(CamcorderProfile.QUALITY_HIGH);
recorder.setProfile(cpHigh);
You need to use:
CamcorderProfile profile = CamcorderProfile.get(Camera.CameraInfo.CAMERA_FACING_FRONT, CamcorderProfile.QUALITY_LOW);
recorder.setProfile(profile);
because CamcorderProfile.QUALITY_HIGH
is reserved for the rear camera.
You will also have to set the video size for mediarecorder as it is in your surface view.
Here is the full example of recording video from front camera with a small preview display:
Android.manifest
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" android:required="false" />
<uses-feature android:name="android.hardware.camera.front" android:required="false" />
activity_camera.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="CameraActivity">
<SurfaceView
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="@+id/surfaceView"/>
<Button
android:layout_width="100dp"
android:layout_height="100dp"
android:text="REC"
android:id="@+id/btnRecord"
android:layout_alignParentBottom="true"
android:layout_marginBottom="25dp" />
</RelativeLayout>
CameraActivity.java
public class SongVideoActivity extends BaseActivity implements SurfaceHolder.Callback {
private int mCameraContainerWidth = 0;
private SurfaceView mSurfaceView = null;
private SurfaceHolder mSurfaceHolder = null;
private Camera mCamera = null;
private boolean mIsRecording = false;
private int mPreviewHeight;
private int mPreviewWidth;
MediaRecorder mRecorder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_song_video);
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread thread, Throwable ex) {
releaseMediaRecorder();
releaseCamera();
}
});
mCamera = getCamera();
//camera preview
mSurfaceView = (SurfaceView) findViewById(R.id.surfaceView);
mSurfaceHolder = mSurfaceView.getHolder();
mSurfaceHolder.addCallback(this);
// deprecated setting, but required on Android versions prior to 3.0
mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
mCameraContainerWidth = mSurfaceView.getLayoutParams().width;
findViewById(R.id.btnRecord).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mIsRecording) {
stopRecording();
} else {
// initialize video camera
if (prepareVideoRecorder()) {
// Camera is available and unlocked, MediaRecorder is prepared,
// now you can start recording
mRecorder.start();
// inform the user that recording has started
Toast.makeText(getApplicationContext(), "Started recording", Toast.LENGTH_SHORT).show();
mIsRecording = true;
} else {
// prepare didn't work, release the camera
releaseMediaRecorder();
// inform user
}
}
}
});
}
private void stopRecording() {
mRecorder.stop(); // stop the recording
releaseMediaRecorder(); // release the MediaRecorder object
mCamera.lock(); // take camera access back from MediaRecorder
// inform the user that recording has stopped
Toast.makeText(this, "Recording complete", Toast.LENGTH_SHORT).show();
mIsRecording = false;
}
@Override
protected void onDestroy() {
super.onDestroy();
releaseMediaRecorder(); // if you are using MediaRecorder, release it first
releaseCamera(); // release the camera immediately on pause event
}
private Camera getCamera() {
Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
for (int camIdx = 0; camIdx < Camera.getNumberOfCameras(); camIdx++) {
Camera.getCameraInfo(camIdx, cameraInfo);
if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
try {
return mCamera = Camera.open(camIdx);
} catch (RuntimeException e) {
Log.e("cameras", "Camera failed to open: " + e.getLocalizedMessage());
}
}
}
return null;
}
@Override
protected void onPause() {
super.onPause();
releaseMediaRecorder(); // if you are using MediaRecorder, release it first
releaseCamera(); // release the camera immediately on pause event
}
private Camera.Size getBestPreviewSize(Camera.Parameters parameters) {
Camera.Size result=null;
for (Camera.Size size : parameters.getSupportedPreviewSizes()) {
if(size.width < size.height) continue; //we are only interested in landscape variants
if (result == null) {
result = size;
}
else {
int resultArea = result.width*result.height;
int newArea = size.width*size.height;
if (newArea > resultArea) {
result = size;
}
}
}
return(result);
}
private boolean prepareVideoRecorder(){
mRecorder = new MediaRecorder();
// Step 1: Unlock and set camera to MediaRecorder
mCamera.unlock();
mRecorder.setCamera(mCamera);
// Step 2: Set sources
mRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
mRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
//recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
// Step 3: Set a CamcorderProfile (requires API Level 8 or higher)
// Customise your profile based on a pre-existing profile
CamcorderProfile profile = CamcorderProfile.get(Camera.CameraInfo.CAMERA_FACING_FRONT, CamcorderProfile.QUALITY_LOW);
mRecorder.setProfile(profile);
// Step 4: Set output file
mRecorder.setOutputFile(new File(getFilesDir(), "movie-" + UUID.randomUUID().toString()).getAbsolutePath());
//recorder.setMaxDuration(50000); // 50 seconds
//recorder.setMaxFileSize(500000000); // Approximately 500 megabytes
mRecorder.setVideoSize(mPreviewWidth, mPreviewHeight);
// Step 5: Set the preview output
mRecorder.setPreviewDisplay(mSurfaceHolder.getSurface());
// Step 6: Prepare configured MediaRecorder
try {
mRecorder.prepare();
} catch (IllegalStateException e) {
Toast.makeText(getApplicationContext(), "exception: " + e.getMessage(), Toast.LENGTH_LONG).show();
releaseMediaRecorder();
return false;
} catch (IOException e) {
Toast.makeText(getApplicationContext(), "exception: " + e.getMessage(), Toast.LENGTH_LONG).show();
releaseMediaRecorder();
return false;
}
return true;
}
private void releaseMediaRecorder(){
if (mRecorder != null) {
mRecorder.reset(); // clear recorder configuration
mRecorder.release(); // release the recorder object
mRecorder = null;
mCamera.lock(); // lock camera for later use
}
}
private void releaseCamera(){
if (mCamera != null){
mCamera.release(); // release the camera for other applications
mCamera = null;
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// The Surface has been created, now tell the camera where to draw the preview.
Camera.Parameters parameters = mCamera.getParameters();
parameters.setRecordingHint(true);
Camera.Size size = getBestPreviewSize(parameters);
mCamera.setParameters(parameters);
//resize the view to the specified surface view width in layout
int newHeight = size.height / (size.width / mCameraContainerWidth);
mSurfaceView.getLayoutParams().height = newHeight;
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
mPreviewHeight = mCamera.getParameters().getPreviewSize().height;
mPreviewWidth = mCamera.getParameters().getPreviewSize().width;
mCamera.stopPreview();
try {
mCamera.setPreviewDisplay(mSurfaceHolder);
} catch (IOException e) {
e.printStackTrace();
}
mCamera.startPreview();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
if (mIsRecording) {
stopRecording();
}
releaseMediaRecorder();
releaseCamera();
}
}
You record audio and video using the same MediaRecorder class. It's pretty simple. Here's an example.
Instead of writing code from the sketch, you can use a library on GitHub. For instance: https://github.com/CameraKit/camerakit-android (or https://github.com/google/cameraview, or https://github.com/hujiaweibujidao/CameraView and so on). Then you only need to:
private CameraKitView cameraKitView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cameraKitView = findViewById(R.id.camera);
}
@Override
protected void onStart() {
super.onStart();
cameraKitView.onStart();
}
@Override
protected void onResume() {
super.onResume();
cameraKitView.onResume();
}
@Override
protected void onPause() {
cameraKitView.onPause();
super.onPause();
}
@Override
protected void onStop() {
cameraKitView.onStop();
super.onStop();
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
cameraKitView.onRequestPermissionsResult(requestCode, permissions, grantResults);
}