Get audio input from microphone in Windows Phone 8

There are numerous apps in the Windows Phone marketplace which let you record audio and play it back. You can build a similar app on your own by following this simple tutorial where we will use Microsoft.Xna.Framework.Audio.Microphone class to take audio input from Windows Phone microphone. Depending on the phone’s processor it may not be possible to access FM radio and microphone at the same time, so if you are planning to build an app that could record FM radio then you much check the specifics before starting. Here are the steps to build the basic functionality.

Step 1. Create a new Windows Phone 8 project for Audio recorder app

Open Visual Studio and choose Windows Phone 8 blank Silverlight application as your app’s template and give it a name. Create project and choose the target platform as Windows Phone 8 (or you may choose WP 8.1 if you wish).

Step 2. Add ID_CAP_MICROPHONE capability to your project

Go to Properties > WMAppManifest.xml and add the capability ID_CAP_MICROPHONE under the capabilities tab. Not adding this capability won’t give a syntax error and the project will build successfully but when you try creating an object of the Microphone class, it will give a null exception.


Step 2. Design the required UI for the app

We will just add two buttons in MainPage.xaml. Record button to start recording. Once the recording starts, we will change its content to “Stop” and use the same button to stop recording. Another button “play” to start playing the recorded audio. Here’s how the MainPage.xaml should look once you add the two buttons to it.

Here’s the XAML you would need to add the two button. Notice that we have click button event handler defined for each of the two buttons.

Step 4. Add the following `using` statements in MainPage.xaml.cs

At the top of the MainPage.xaml.cs file add the following using statements. They will be required for the code we are going to add.

Step 5. Add some global members in the MainPage.xaml.cs file

We would require some global variables to use Microphone, memory stream and SoundEffect classes. Add it before the MainPage constructor so that all the methods can use it.

Step 6. Add the following code in MainPage’s constructor

In the constructor of MainPage.xaml add the following code. Since we are using XNA Game Studio in a Windows Phone app, we need to simulate the Game loop that XNA Game Studio normally implements for us. Also we add Microphone.BufferReady event handler to the constructor of your MainPage class.

The app’s MainPage class should look something similar to this after you have added the code in Step 4, Step 5 and Step 6.

Step 7. Add the code for BufferReady event handler.

This handler copies the data from the microphone into a buffer and writes that buffer to a stream.

Step 8. Add the code to handler recording (Start and Stop)

In the record button click event handler we add the code to start and stop recording. We first create an object of Button class to get the content of the button which triggered the click event. Next we check if the content is “Record”, then we start recording else we stop recording.

Note:

  • This event handler for a record button Click event allocates a buffer large enough to hold 1 second of audio and begins collecting that data by calling Microphone.Start.
  • This event handler for a stop button Click event checks to see whether the microphone is currently collecting input. If it is, the code calls Microphone.Stop to end the recording.

Step 9. Add the code to play the recorded audio

This event handler for a play button Click event allocates a new SoundEffect object using the stream where the audio data was saved. Then, it calls the SoundEffect.Play method.

That’s it. Try running the app in the emulator and see if it works.

Here’s the full source code of the project to record and play audio from microphone,

Download full project source Record-Audio.zip