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).
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.
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.
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?
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.
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").
Step 7
You need to edit the Second Button Id value and Text value (Ex:android:id="@+id/Stop" android:text="Stop").
Step 8
You need to edit the Third Button Id value and Text value (Ex:android:id="@+id/Play" android:text="Play").
Step 9
In this step, you need to drag and drop the VideoView.
Step 10
Now, go to the properties Window. You need to edit the Video View Id value (Ex:android:id="@+id/SampleVideoView" ).
Step 11
In this step, go to the Main.axml page Source Panel. Note the Id value.
Main.axml - <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent">
- <Button android:id="@+id/Record" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Record" />
- <Button android:id="@+id/Stop" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Stop" />
- <Button android:id="@+id/Play" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Play" />
- <VideoView android:id="@+id/SampleVideoView" android:layout_width="fill_parent" android:layout_height="fill_parent" />
- </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; Step 13
MainActivity.cs In this step. you need to write the code, given below from OnCreate() method.
- protected override void OnCreate(Bundle bundle)
- {
- base.OnCreate(bundle);
-
- SetContentView(Resource.Layout.Main);
- string path = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath + "/test.mp4";
- var record = FindViewById < Button > (Resource.Id.Record);
- var stop = FindViewById < Button > (Resource.Id.Stop);
- var play = FindViewById < Button > (Resource.Id.Play);
- var video = FindViewById < VideoView > (Resource.Id.SampleVideoView);
- record.Click += delegate {
- video.StopPlayback();
- 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();
- };
- stop.Click += delegate {
- if (recorder != null) {
- recorder.Stop();
- recorder.Release();
- }
- };
- play.Click += delegate {
- var uri = Android.Net.Uri.Parse(path);
- video.SetVideoURI(uri);
- video.Start();
- };
- }
- protected override void OnDestroy() {
- base.OnDestroy();
- if (recorder != null) {
- recorder.Release();
- recorder.Dispose();
- recorder = null;
- }
- }
- }
-
- if ('this_is' == /an_example/) {
- of_beautifier();
- } else {
- var a = b ? (c % d) : e[f];
- }
Step 14
In this step, give the required permissions in your app.
Go to Solution Explorer--> properties-->right click Open.
Step 15
After opening the properties options, select Android Manifest-->Required Permissions-->Check CAMERA.
Step 16
Select Android Manifest-->Required Permissions-->Check WRITE_EXTERNAL_STORAGE.
Step 17
Select Android Manifest-->Required Permissions-->Check RECORD_AUDIO.
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.
Output
After a few seconds, the app will start running on your phone.
If you click Record button, the video will start to record.
Click Stop button and video record will stop.
Click Play button. The recorded video will be played.
Summary Thus, this was the process of how to record a video in Xamarin Android app, using Visual Studio 2015.