How To Record Audio 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.

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).

New  Project

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.

New  Project

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.

New  Project

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.

New  Project

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.

New  Project

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").

New  Project

Step 7

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

New  Project

Step 8

You need to change second button enabled value (Ex: android:enabled="false").

New  Project

Step 9

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

Main.axml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent">  
  3.     <Button android:id="@+id/start" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Start Recording" />  
  4.     <Button android:id="@+id/stop" android:layout_width="fill_parent" android:layout_height="wrap_content" android:enabled="false" android:text="Stop Recording" />  
  5. </LinearLayout>  
New  Project

Step 10

Now, go to the MainActivity.cs page. Write the code, given below.
  1. MainActivity.cs  
  2. using Android.Media;  
  3. using System.IO;  
  4. Add class variables  
  5. for a MediaRecorder and MediaPlayer.and Also add class variables  
  6. for the buttons so we can start and stop.  
  7. MediaRecorder _recorder;  
  8. MediaPlayer _player;  
  9. Button _start;  
  10. Button _stop;  
New  Project

Step 11

In this step, you will write the code, given below from OnCreate() method:in MainActivity.cs.
  1. protected override void OnCreate(Bundle bundle) {  
  2.     base.OnCreate(bundle);  
  3.     // Set our view from the "main" layout resource  
  4.     SetContentView(Resource.Layout.Main);  
  5.     _start = FindViewById < Button > (Resource.Id.start);  
  6.     _stop = FindViewById < Button > (Resource.Id.stop);  
  7.     string path = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath + "/test.3gpp";  
  8.     _start.Click += delegate {  
  9.         _stop.Enabled = !_stop.Enabled;  
  10.         _start.Enabled = !_start.Enabled;  
  11.         _recorder.SetAudioSource(AudioSource.Mic);  
  12.         _recorder.SetOutputFormat(OutputFormat.Default);  
  13.         _recorder.SetAudioEncoder(AudioEncoder.Default);  
  14.         _recorder.SetOutputFile(path);  
  15.         _recorder.Prepare();  
  16.         _recorder.Start();  
  17.     };  
  18.     _stop.Click += delegate {  
  19.         _stop.Enabled = !_stop.Enabled;  
  20.         _recorder.Stop();  
  21.         _recorder.Reset();  
  22.         _player.SetDataSource(path);  
  23.         _player.Prepare();  
  24.         _player.Start();  
  25.     };  
  26. }  
  27. protected override void OnResume() {  
  28.     base.OnResume();  
  29.     _recorder = new MediaRecorder();  
  30.     _player = new MediaPlayer();  
  31.     _player.Completion += (sender, e) => {  
  32.         _player.Reset();  
  33.         _start.Enabled = !_start.Enabled;  
  34.     };  
  35. }  
  36. protected override void OnPause() {  
  37.     base.OnPause();  
  38.     _player.Release();  
  39.     _recorder.Release();  
  40.     _player.Dispose();  
  41.     _recorder.Dispose();  
  42.     _player = null;  
  43.     _recorder = null;  
  44. }  
New  Project

Step 12

In this step, give Required Permissions in your app.

Go to Solution Explorer--> Properties-->Right click on Open.

New  Project

Step 13

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

New  Project

Step 14

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

New  Project

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.

New  Project

Output

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

If you click START RECORDING, audio will start to record.

New  Project

If click STOP RECORDING. Recorded Audio will be Play.

New  Project

Summary

Hence, this was the process of how to record an audio in Xamarin Android app, using Visual Studio 2015

 

Up Next
    Ebook Download
    View all
    Learn
    View all