1

I am using following code in an Android application written in Xamarin:

var recorder = new MediaRecorder ();
recorder.SetVideoSource (VideoSource.Camera); 
recorder.SetAudioSource (AudioSource.Mic);              
recorder.SetOutputFormat (OutputFormat.Default);
recorder.SetVideoEncoder (VideoEncoder.Default); 
recorder.SetAudioEncoder (AudioEncoder.Default);      
recorder.SetOutputFile (path);       
recorder.SetPreviewDisplay (video.Holder.Surface);         
recorder.Prepare ();
recorder.Start ();

It works but i have a problem. It uses background camera and orientation is landscape. How can I use front facing camera for recording the video and set orientation to portrait ?

Kartik
  • 1,506
  • 2
  • 17
  • 28

3 Answers3

1

Source: Android can't record video with Front Facing Camera, MediaRecorder start failed: -19

First, make sure that your permissions are set up correctly. Specifically, to record video, you'll want:

 <uses-feature android:name="android.hardware.camera.front" />
 <uses-feature android:name="android.hardware.microphone"/>

 <uses-permission android:name="android.permission.CAMERA" />
 <uses-permission android:name="android.permission.RECORD_AUDIO" />
 <uses-permission

android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> Second, and this is the tricky part, this line from the tutorial does not work with the front-facing camera!

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

That signature for CamcorderProfile.get() defaults to a profile for the back-facing camera:

Returns the camcorder profile for the first back-facing camera on the device at the given quality level. If the device has no back-facing camera, this returns null.

Instead, use http://developer.android.com/reference/android/media/CamcorderProfile.html#get(int,%20int). The first parameter is the id of the camera that you opened, specifically, the front-facing camera.

Community
  • 1
  • 1
Johan
  • 8,068
  • 1
  • 33
  • 46
  • The suggested API CamcorderProfile.get(int, int) is not present in xamarin – Kartik Oct 01 '14 at 19:33
  • the equivalent is CamcorderProfile.get(int, CamcorderQuality). when mapping the native APIs, Xamarin tries to replace dumb Android int constants with enums where they can (which is awesome) – Benoit Jadinon Oct 01 '14 at 20:03
  • What i meant was: CamcorderProfile.Get (1, CamcorderQuality) throws an error in Xamarin - 'No overload for method 'Get' takes 2 arguments' – Kartik Oct 02 '14 at 03:52
1

This is how you do it:

First make sure your minimum supported API version is Gingerbread 2.3

Second ensure all permissions have been added to manifest file

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

And use this snippet to get reference to front camera and display the preview in video view while recording

if (Camera.NumberOfCameras < 2) {
        Toast.MakeText (this, "Front camera missing", ToastLength.Long).Show ();
        return;
}

_camera = Camera.Open (1);
_camera.SetDisplayOrientation (90);
_camera.Unlock ();

recorder = new MediaRecorder ();

recorder.SetCamera (_camera);

recorder.SetAudioSource (AudioSource.Mic);    
recorder.SetVideoSource (VideoSource.Camera); 

recorder.SetOutputFormat (OutputFormat.Default);
recorder.SetAudioEncoder (AudioEncoder.Default);
recorder.SetVideoEncoder (VideoEncoder.Default);

recorder.SetOutputFile (path);       

recorder.SetOrientationHint (270);

recorder.SetPreviewDisplay (previewVideoView.Holder.Surface);         
recorder.Prepare ();
recorder.Start ();
Kartik
  • 1,506
  • 2
  • 17
  • 28
0

I used @Kartik's solution with a small modification. Not all devices have a rear facing camera(2012 Nexus 7 is a great example). This verison of the code goes through all available cameras and checks if the camera is truly the front one.

for (int camIndex = 0; camIndex < Camera.NumberOfCameras; camIndex++)
{
  Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
  Camera.GetCameraInfo(camIndex, cameraInfo);
  if (cameraInfo.Facing == CameraFacing.Front)
  {
    try
    {
      _camera = Camera.Open(camIndex);
      _camera.SetDisplayOrientation(270);
      _camera.Unlock();
     }
     catch (Exception e)
     {
       Toast.MakeText(this, "Front camera missing", ToastLength.Long).Show();
     }
   }
 }
Chris Woolum
  • 2,854
  • 20
  • 20