Sound Recording

Must Read

Working with Client-Side Data

We have talked about HTML5 and CSS3 markup, but now let's turn our attention to some of  the technologies...

Five Tips to Help Stay on Top of Your College Debt

College just isn't for frat parties, and drinking. It does serve to set you up for life in a...

Making Your Link the Place to Click

There are links and then there are links. Usually links are your company name hyperlinked to your home page,...

All Windows Phone 7 physical devices include a microphone. You can access this using the methods of the XNA Framework. This is more complex than using some other features of the phone because you must initiate an asynchronous dispatcher to raise the microphone events.

To record sounds from the microphone, you must do the following:

  • Add a reference to the assembly Microsoft.Xna.Framework to your project.
  • Create an XNA asynchronous dispatcher “pump” class and initialize it in the constructor or startup event in your App.xaml. cs file.
  • Get a reference to a microphone.
  • Specify a function or lambda expression that will receive the sound data.
  • Start the microphone.

Note: An XNA asynchronous event dispatcher is an application service that allows Silverlight to detect events raised by XNA components. Usually, these events are propagated by the XNA game loop mechanism, but this is not present in a Silverlight application. The dispatcher service collects these events and raises them through the Silverlight event mechanism.

In your application, declare variables to hold the microphone instance and a stream that will receive the sound data bytes. This example uses the default microphone and an isolated storage file stream. To use this code, you must reference the namespaces Microsoft.Xna. Framework.Audio and System.IO.IsolatedStorage.

Microphone mic = Microphone.Default;
IsolatedStorageFileStream myFileStream;

Specify the callback for the BufferReady event and the size of the recording buffer. The maximum size is one second, though lower values tend to give better performance.

mic.BufferReady += Mic_BufferReady;
mic.BufferDuration = TimeSpan.FromMilliseconds(500);

Open the file stream that will receive the recorded data (in this case, an isolated storage file), and then start the microphone to begin recording.

// Create a virtual store and file stream.
// Check for an existing duplicate name.
var myStore = IsolatedStorageFile.GetUserStoreForApplication();
String fileName = “MyAudio.dat”;
if (myStore.FileExists(fileName))
myFileStream = myStore.CreateFile(fileName);

The callback for the BufferReady event must capture the data from the microphone and store it in your target location.

void Mic_BufferReady(object sender, EventArgs e)
Microphone mic = sender as Microphone;
byte[] content = new byte[mic.GetSampleSizeInBytes
myFileStream.Write(content, 0, content.Length);

To stop recording, call the Stop method of the Microphone class and close the stream you are writing to.


You can check the status of the microphone at any time by querying the State property of the Microphone class. This returns a value from the MicrophoneState enumeration. Possible values are Started and Stopped.

For more information, see “Microphone Class” on MSDN (

Note: The files created from the byte stream returned by the Microphone class are not valid sound files. If you want to convert a saved byte stream as a .wav or .wmv file, you must use an auxiliary class or library to add the appropriate headers to the file and save it is the appropriate format. For a description of the .wav file format, see “Wave File Format” on The Sonic Spot website (

The following series of posts by Microsoft academic developer evangelist Dan Waters describe the techniques of audio programming and provide code you can reuse:

“Intro to Audio Programming, Part 1: How Audio Data is Represented” at

“Intro to Audio Programming, Part 2: Demystifying the WAV Format” at

“Intro to Audio Programming, Part 3: Synthesizing Simple Wave Audio using C#” at

“Intro to Audio Programming Part 4: Algorithms for Different Sound Waves in C#” at



Previous articleSearch
Next articleSound Playback


Please enter your comment!
Please enter your name here

Latest News

Underlight As Accent, For Power and The Main Light for Photography

Underlighting, in which fill or accent light comes from under the topic, is not widely used technique in the traditional Portraits,...

How To Fix Overexposure As A Creative Tool, The Complete Guide

As an creative tool, overexposure is sort of underrated. What I’m close to propose could be a deliberate and well thought out technique for...

7D Mark II is Canon’s best DSLR cameras without full-frame sensor.

The expected long-awaited Canon EOS 7D Mark II are shipping in November for $ 1,799 without a lens. With a higher price tag, you...

Low key photography and How to isolate your subject.

Low key photography doesn’t depend on underexposure to make its point; the key to low key is that the majority of tones, even correctly...

High Key Lighting Techniques for Professional Photographers

I’ve written many times about high key lighting techniques and how to achieve them. The term “high key” is a bit misleading. As I’ve...

More Articles Like This