Sound Recording


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