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.
Audio Record control is used to record audio, using a Mic.
Prerequisites
- Visual Studio 2015 Update 3
The steps are given below in order to record audio in an Android app, using Visual Studio 2015.
Step 1
Click File--> select New--> select Project. The project needs to be clicked, after opening all types of projects in Visual Studio
or click (Ctrl+Shift+N).
Step 2
After opening New Project, select Installed-->Templates-->Visual C#-->Android-->choose the blank App (Android).
Now, give your Android app; a name (Ex:sample) and give the path of your project. Afterwards, click OK.
Step 3
Now, go to Solution Explorer. In Solution Explorer, get all the files and sources in your project.
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 the page, as per your wish.
Now, delete the Linear Layout and default hello world button. Go to the source panel. You can see the button coding. You need to delete it.
After deleting XAML code, now 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. You will see all the tools and control.
You need to drag and drop the two buttons.
Step 6
Now, go to the properties Window. You need to edit the First button's Id value and Text value (Ex:android:id="@+id/start" android:text="Start Recording").
Step 7
You need to edit the Second button's Id value and Text value (Ex:android:id="@+id/Stop" android:text="Stop Recording").
Step 8
You need to change second button enabled value (Ex: android:enabled="false").
Step 9
In this step, go to the Main.axml page Source Panel. Note the button's Id value.
Main.axml
- <?xml version="1.0" encoding="utf-8"?>
- <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/start" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Start Recording" />
- <Button android:id="@+id/stop" android:layout_width="fill_parent" android:layout_height="wrap_content" android:enabled="false" android:text="Stop Recording" />
- </LinearLayout>
Step 10
Now, go to the MainActivity.cs page. Write the code, given below.
- MainActivity.cs
- using Android.Media;
- using System.IO;
- Add class variables
- for a MediaRecorder and MediaPlayer.and Also add class variables
- for the buttons so we can start and stop.
- MediaRecorder _recorder;
- MediaPlayer _player;
- Button _start;
- Button _stop;
Step 11
In this step, you will write the code, given below from OnCreate() method:in MainActivity.cs.
- protected override void OnCreate(Bundle bundle) {
- base.OnCreate(bundle);
-
- SetContentView(Resource.Layout.Main);
- _start = FindViewById < Button > (Resource.Id.start);
- _stop = FindViewById < Button > (Resource.Id.stop);
- string path = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath + "/test.3gpp";
- _start.Click += delegate {
- _stop.Enabled = !_stop.Enabled;
- _start.Enabled = !_start.Enabled;
- _recorder.SetAudioSource(AudioSource.Mic);
- _recorder.SetOutputFormat(OutputFormat.Default);
- _recorder.SetAudioEncoder(AudioEncoder.Default);
- _recorder.SetOutputFile(path);
- _recorder.Prepare();
- _recorder.Start();
- };
- _stop.Click += delegate {
- _stop.Enabled = !_stop.Enabled;
- _recorder.Stop();
- _recorder.Reset();
- _player.SetDataSource(path);
- _player.Prepare();
- _player.Start();
- };
- }
- protected override void OnResume() {
- base.OnResume();
- _recorder = new MediaRecorder();
- _player = new MediaPlayer();
- _player.Completion += (sender, e) => {
- _player.Reset();
- _start.Enabled = !_start.Enabled;
- };
- }
- protected override void OnPause() {
- base.OnPause();
- _player.Release();
- _recorder.Release();
- _player.Dispose();
- _recorder.Dispose();
- _player = null;
- _recorder = null;
- }
Step 12
In this step, give Required Permissions in your app.
Go to Solution Explorer--> Properties-->Right click on Open.
Step 13
After opening the properties options, select Android Manifest-->Required Permissions-->Check RECORD_AUDIO.
Step 14
Select Android Manifest-->Required Permissions-->Check WRITE_EXTERNAL_STORAGE.
Step 15
If you have an 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 the 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 START RECORDING, audio will start to record.
If click STOP RECORDING. Recorded Audio will be Play.
Summary Hence, this was the process of how to record an audio in Xamarin Android app, using Visual Studio 2015