Intent to take video in android

2019-04-07 07:20发布

问题:

I need to take video from my application using only front camera. I am using intent to perform this action.

Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, videoUri);
intent.putExtra("android.intent.extra.durationLimit", 30);
intent.putExtra("android.intent.extras.CAMERA_FACING", 1); //to open front facing camera
startActivityForResult(intent, VIDEO_CAPTURE);

When I run the application, I am able to take video using front camera. But suppose when I click my record video button and the camera view is opened. In that user go and change the camera to rear camera, then always my intent is opening rear camera only after that. Its not taking the line

intent.putExtra("android.intent.extras.CAMERA_FACING", 1);

Could someone please tell me whats the issue and is it able to be solved using intent?

回答1:

There is no reliable way to use intent to show the front camera all the time at least not on all devices. Only way to reliably do it is to create a SurfaceView and capture the video yourself.



回答2:

See if this works :

try {
    if (Camera.getNumberOfCameras() == 2) {
        if (frontCamera) {
            frontCamera = false;
            prCamera.stopPreview();
            prMediaRecorder.release();
            prMediaRecorder = null;
            prCamera.release();
            prCamera = null;
        } else {

            frontCamera = true;
            prCamera.stopPreview();
            prMediaRecorder.release();
            prMediaRecorder = null;
            prCamera.release();
            prCamera = null;
        }
        Intent intent = new Intent(VideoCapture_New.this,
                VideoCapture_New.class);
        startActivity(intent);
    } else {
        Toast.makeText(VideoCapture_New.this,
                "Your device doesn't contain Front Camera.",
                Toast.LENGTH_SHORT).show();
    }
} catch (Exception e) {
    e.printStackTrace();
    Toast.makeText(VideoCapture_New.this,
            "Your device is not compatible for Front Camera.",
            Toast.LENGTH_SHORT).show();

}

source : Front camera in android

Else you could use Android keyEvents to trigger the button press of camera switch if video starts to record on back camera. KeyEvents need to timed perfectly otherwise they end triggering something else! Check : KeyEvent.

Also if you are making use of

mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));

This signature for CamcorderProfile.get() defaults to a profile for the back-facing camera. So instead of using this, use :

public static CamcorderProfile get (int cameraId, int quality)

mediaRecorder.setVideoFrameRate(15);

use any value, 1 - 15 for frame rate. check this for additional details.

Hope this helps.



回答3:

Create your custom video taking application that assure to only use the Front camera

i think this is the only way to do this .

Hope that Helps .