How To Record Video In Xamarin Android App Using Visual Studio 2015

Introduction

Xamarin is a platform to develop the cross-platform and multi-platform apps. (Ex. Windows phone, Android, iOS) in Xamarin platform, where code sharing concept is used. In Xamarin studio, Visual Studio is also available.

Video Record control is used to how to record video, using camera and Mic.

Prerequisites

  • Visual Studio 2015 Update 3

The steps, given below need to be followed in order to record video in Android app, using Visual Studio 2015.

Step 1

Click file--> Select New--> select Project. The project needs to be clicked after opening all the types of projects in Visual Studio 
or click (Ctrl+Shift+N).

xamarin

Step 2

After opening the New Project, select Installed-->Templates-->Visual C#-->Android-->choose the Blank app (Android).

Now, give your Android app name (Ex:sample) and give path of your project. Afterwards, click OK.

Xamarin

Step 3

Now, go to Solution Explorer. In Solution Explorer, get all the files and the source in your project.

Now, select Resource-->Layout--> double click to open main.axml page. You need to select the source to write XAML code.

If you want to design, choose the designer Window and you can design your app.

Xamarin

Step 4

After opening, main.axml file will open the main page designer. In this page, you can design this page, which type do you want?


Xamarin

Now, delete the Linear Layout and Default hello world button. Go to the source panel and you can see the button coding. You need to delete it.

After deleting XAML code, delete C# button action code.

Go to the MainActivity.cs page. You need to delete the button code.

Step 5

Now, go to the toolbox Window in the toolbox Window. Get all types of the tools and control. You will go to the toolbox Window. Now, scroll down and you will see all the tools, control.

You need to drag and drop the three buttons.

Xamarin

Step 6

Now, go to the properties Window. You need to edit the First Button Id value and Text value (Ex:android:id="@+id/Record" android:text="Record").

Xamarin

Step 7

You need to edit the Second Button Id value and Text value (Ex:android:id="@+id/Stop" android:text="Stop").

Xamarin

Step 8

You need to edit the Third Button Id value and Text value (Ex:android:id="@+id/Play" android:text="Play").

Xamarin

Step 9

In this step, you need to drag and drop the VideoView.

Xamarin

Step 10

Now, go to the properties Window. You need to edit the Video View Id value (Ex:android:id="@+id/SampleVideoView" ).

Xamarin

Step 11

In this step, go to the Main.axml page Source Panel. Note the Id value.



Main.axml
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent">  
  2.     <Button android:id="@+id/Record" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Record" />  
  3.     <Button android:id="@+id/Stop" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Stop" />  
  4.     <Button android:id="@+id/Play" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Play" />  
  5.     <VideoView android:id="@+id/SampleVideoView" android:layout_width="fill_parent" android:layout_height="fill_parent" />  
  6. </LinearLayout>  
Step 12

Now, go to the MainActivity.cs page. Write the code, given below.

MainActivity.cs

Using Android.Media;

In the Activity, add a class variable for the MediaRecorder.

MediaRecorder recorder;

Xamarin

Step 13

MainActivity.cs

In this step. you need to write the code, given below from OnCreate() method.
  1. protected override void OnCreate(Bundle bundle)  
  2. {    
  3.     base.OnCreate(bundle);    
  4.     // Set our view from the "main" layout resource    
  5.     SetContentView(Resource.Layout.Main);    
  6.     string path = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath + "/test.mp4";    
  7.     var record = FindViewById < Button > (Resource.Id.Record);    
  8.     var stop = FindViewById < Button > (Resource.Id.Stop);    
  9.     var play = FindViewById < Button > (Resource.Id.Play);    
  10.     var video = FindViewById < VideoView > (Resource.Id.SampleVideoView);    
  11.     record.Click += delegate {    
  12.         video.StopPlayback();    
  13.         recorder = new MediaRecorder();    
  14.         recorder.SetVideoSource(VideoSource.Camera);    
  15.         recorder.SetAudioSource(AudioSource.Mic);    
  16.         recorder.SetOutputFormat(OutputFormat.Default);    
  17.         recorder.SetVideoEncoder(VideoEncoder.Default);    
  18.         recorder.SetAudioEncoder(AudioEncoder.Default);    
  19.         recorder.SetOutputFile(path);    
  20.         recorder.SetPreviewDisplay(video.Holder.Surface);    
  21.         recorder.Prepare();    
  22.         recorder.Start();    
  23.     };    
  24.     stop.Click += delegate {    
  25.         if (recorder != null) {    
  26.             recorder.Stop();    
  27.             recorder.Release();    
  28.         }    
  29.     };    
  30.     play.Click += delegate {    
  31.         var uri = Android.Net.Uri.Parse(path);    
  32.         video.SetVideoURI(uri);    
  33.         video.Start();    
  34.     };    
  35. }    
  36. protected override void OnDestroy() {    
  37.     base.OnDestroy();    
  38.     if (recorder != null) {    
  39.         recorder.Release();    
  40.         recorder.Dispose();    
  41.         recorder = null;    
  42.     }    
  43. }    
  44. // This is just a sample script. Paste your real code (javascript or HTML) here.    
  45.     
  46. if ('this_is' == /an_example/) {    
  47.     of_beautifier();    
  48. else {    
  49.     var a = b ? (c % d) : e[f];    
  50. }    
Xamarin

Step 14

In this step, give the required permissions in your app.

Go to Solution Explorer--> properties-->right click Open.

Xamarin

Step 15

After opening the properties options, select Android Manifest-->Required Permissions-->Check CAMERA.

Xamarin

Step 16

Select Android Manifest-->Required Permissions-->Check WRITE_EXTERNAL_STORAGE.

Xamarin

Step 17

Select Android Manifest-->Required Permissions-->Check RECORD_AUDIO.

Xamarin

Step 18

If you have Android Virtual device, run the app on it, else connect your Android phone and run the app on it.

Simply, connect your phone and go to Visual Studio. The connected phone will show up in Run menu (Ex:LENOVO A6020a40(Android 5.1-API 22)). Click the Run option.

Xamarin

Output

After a few seconds, the app will start running on your phone.

If you click Record button, the video will start to record.

Xamarin

Click Stop button and video record will stop.

Xamarin

Click Play button. The recorded video will be played.

Xamarin

Summary

Thus, this was the process of how to record a video in Xamarin Android app, using Visual Studio 2015. 


Similar Articles