Recording and Saving an Audio file in Windows 10 Application

In this post, I will start with recording audio files and saving them in the storage. So, for recording audio files, we need to add the support for Microphone in the application.

Go to package.appxmanifest file -> Capabilities -> Check on Microphone and Music Library.

After adding the capabilities to use Microphone and Music Library to record and save the data, respectively. We will now move on to adding the code which will be added in the XAML file of the application, which is:

 

After adding the code in the XAML file, we will add the code for the functionalities of Buttons: Save, Stop and Record. But before adding any code in the .CS file of the page, we will add few namespaces which we will be utilizing in the application for Recording and Saving the audio. These namespaces are:

 

Now, we will create enumeration type for utilizing audio formats and audio quality in which to save the audio. This enumeration will helps to save recorded audio in the specified format. The code to declare enum type is below:

Now, we will add a static class ‘AudioEncodingFormatExtensions‘ to utilize the enum type, we had declared above. The class is static because we don’t need to use any object for it, rather we just need to utilize a method to be executed throughout the application. The code for the class is:

 

Now, we will declare the variables which we are required to use during the span of the application. The declarations are as follows:

 

As we have declared all the variables to be used, now, we will start defining methods and adding the functionalities to the methods. At first, we will add some code to the OnNavigatedTo event of the page. The code being added is the one which gets executed when the page is loaded first time, this code is:

 

Now, the methods which are called in the code above are as follows:

 

Now, we will be adding the code to initialize timer and associate it with an event which will be called for the timer is ticking/counting. The code is below:

 

If the audio getting recorded exceeds the limit of the audio capture, we should be able to handle such situations. For handling such situation, we had added a declaration to call an event when media capture limit is exceeded. The code for that event is as below:

 

Now, we will add code for Media capture fail event which will be called when there is any failure during the capturing of audio. The code is as follows:

 

As of now, we have defined all the functionalities which will be working in the back end  to capture the media. Now, we will add the code to the button which on clicking will call these events and starts capturing the audio. The code for audio recording button click event is:

 

After we have added the functionality for the Record click event, now, we will be adding functionality for Stop button click event along with the functionality for Save button which will open a Save File Dialog box to save the file at particular location in the system(Here, default is set to the ). The code is as below:

 

We have added the comboboxes for selecting which format we will be using and which quality the audio will be saved into. Now, here is the code for the selected item method for those comboboxes:

 

Here, in the above code, we have called a method InitFileSavePicker() which is used to initialize the variables for file save dialog box and it’s options. The code for this method is as follows:

 

Now, we are done with coding our simple Audio Recorder Application. 

Run the application and enjoy!

Download the application in the link below:

[ LINK TO DOWNLOAD ]