How to record video from front facing camera?

PlayportTeamPlayportTeam INMember
edited September 2014 in Xamarin.Android

I am using following code:

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 ?

Answers

  • PlayportTeamPlayportTeam INMember

    Any take on this?

  • PlayportTeamPlayportTeam INMember
    edited October 2014

    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 ();
    
Sign In or Register to comment.