Pete On Software

RSS Feed

Windows Phone 7 – Microphone and Isolated Storage

In my last Windows Phone 7 blog entry, we took a look at making a very simple Windows Phone 7 application. This time around, our goal is to make an app that uses the microphone on the phone and reads and writes to the phone’s isolated storage.

Let’s fire up Microsoft Visual Studio 2010 Express for Windows Phone and go File–>new Project–>Visual C#–>Silverlight for Windows Phone–>Windows Phone Application. Pick a Location for the Solution and name it SoundRecorder.

File New Project

The first thing we need to do after the project is created is to add a reference to Microsoft.Xna.Framework.dll. Right click on References in the Solution Explorer and select “Add Reference”. Find Microsoft.Xna.Framework.dll, select it, and click “Ok”. That will reference the Xna Framework (even though this is a Silverlight application) and give us easy access to the microphone.

Adding the Microsoft.Xna.Framework reference

Now, let’s get our XAML set up. Type / copy-paste / set up the code you see below into MainPage.xaml. There isn’t much to see here besides three buttons (one of them disabled at the start) and our app’s title. Due to space constraints and the desire to be very “2.0”, I had to shorten the application name in the title to “Sound Recordr” from “Sound Recorder”.

Now, in the code behind (MainPage.xaml.cs), we should have the following:

Examine this particular line:

This code gives us a new Microsoft.Xna.Framework.Audio.Microphone object. We call for Microphone.Default and that gives us the default microphone on the system. The great thing about this is that on the phone is is the phone’s microphone, headset, etc, but in our simulator – your computer’s microphone is used. All of the other things that are done with this class are just the built-in XNA magic (documentation).

The only other piece we use here is the isolated storage. I get a reference to it in this line:

After that, I basically use my variable userStore just like I would any other component in the System.IO namespace. That is really the power of Windows Phone 7 development. All of this code should be very familiar to .Net developers and behaves just like the phone was a little computer (which of course it is).

When we run our application, we see the following:
Initial State of our Sound Recordr Application

We cannot play the file yet because we haven’t recorded one. If we click “Start” and just talk or sing or make noise for the mic and then click “Stop”, we see that the “Play Existing File” button becomes activated.
Sound Recordr after Recording

If you click “Play Existing File”, the file will be retrieved out of storage and played back with another excellent class from Microsoft.Xna.Framework.Audio, the SoundEffect class.

This is definitely demo code (read: it has bugs), and is only guaranteed to work on my machine and with the April CTP bits that I have loaded. Disclaimers aside, this definitely shows that developing for Windows Phone 7 should be extremely easy for anyone who knows or can learn .Net.

7 Comments

Ibrahim Ersoy  on August 4th, 2010

Hi Pete,

I have made the XNA version of Microphone sample using your codes.

I have written an article about it.

Do i have permission to publish it by telling about you in the article?

Thank you

Pete  on August 5th, 2010

Ibrahim,

Of course! I think that’s awesome and I look forward to reading the article.

Ibrahim Ersoy  on August 13th, 2010

Hi Pete,
You can read it from here:
http://www.c-sharpcorner.com/UploadFile/iersoy/2550/Default.aspx?ArticleID=c7010c16-2a9f-4761-b5c6-31e3cdf75a31

The same way you build application in WP7 using Silverlight is being ported to XNA with no pain as you will see in the article.

Thank you!
Have a nice day!

Cheers,
Ibrahim

Pete  on August 18th, 2010

Great article, Ibrahim, and thanks for the shout-out.

Michael Sync  on October 26th, 2010

I think you will get this error with latest tool..

FrameworkDispatcher.Update has not been called. Regular FrameworkDispatcher.Update calls are necessary for fire and forget sound effects and framework events to function correctly. See http://go.microsoft.com/fwlink/?LinkId=193853 for details.

nagarajan  on May 10th, 2012

FrameworkDispatcher.Update has not been called. Regular FrameworkDispatcher.Update calls are necessary for fire and forget sound effects and framework events to function correctly. See http://go.microsoft.com/fwlink/?LinkId=193853 for details.

nagarajan  on May 10th, 2012

Hai Michael Sync i have a same problem please help me sir

Leave a Comment