Did you know? DZone has great portals for Python, Cloud, NoSQL, and HTML5!
Windows Phone Zone is brought to you in partnership with:

Jerry Nixon is the Microsoft Developer Evangelist in Colorado. He has scary knowledge about Architecture, XAML, and SQL Server. He has terrifying knowledge about Windows Phone, the Kinect, and C#. And, he has simply ridiculous knowledge on Windows Desktop development in Windows 8. Jerry is a DZone MVB and is not an employee of DZone and has posted 58 posts at DZone. You can read more from them at their website. View Full User Profile

Windows Phone: A Microphone-enabled Phone App in 5 Minutes!

02.15.2012
Email
Views: 784
  • submit to reddit
It's so easy to get up and running with your first Windows Phone app!  Just pick up the FREE toolkit from Microsoft, register at AppHub, and start checking out some fundamental tutorials.

There are a lot of sensors on the Windows Phone. We’ve got:

  1. Microphone
  2. GPS Location
  3. Camera (front & back)
  4. Gyroscope / Accelerometer
  5. Magnetometer / Compass


I suppose we could also say the touch panel is a sensor, but it is so ubiquitous that we just assume it’s there anymore – yes, it’s that world we live in now, the “of course it’s touch enabled” world. Wonderful.

But how do you use the Microphone?

The Microphone

The Windows Phone microphone is in the Microsoft.Xna.Framework.Audio namespace. This means in order to access the microphone, you need to add a reference to Microsoft.Xna.Framework. Easy enough, I know – but you definitely have to do it.

Listen

The microphone is a mono input device. To “listen” with the microphone, you Start() the microphone and fill its buffer. Then while handling the BufferReady event, you read the buffer and append it to some stream (like a MemoryStream). It’s the stream that contains the audio; it’s the stream you can save to a file. The microphone’s state will remain Started until you call Stop().

Watch the Video!


In the video linked above, we build a full application that will record from the microphone and play back whatever you record. (I know in the video you cannot hear the playback but it worked!)

Note: One important part of the code in the video is calling the FrameworkDispatcher.Update() method. This method is called automatically in XNA game classes. However, this is a Silverlight class so we must call it manually. The purpose of the method is to dispatch events that update XNA components and bring them to a ready (or other) state – this includes the microphone. 33 milliseconds is a recommended tick value.


You can get the raw code from the video here.

Conclusion

Building an application in Windows Phone is fast. The pieces you need are at arms reach, and the necessary plumbing is mostly built for you. The microphone is another way to let your user interact with your application – and another way to bring value from your application – and another way to fight against the UNINSTALL button. It may be simple, but is is very powerful.

Warning: every application on the Windows Phone has a memory limit of 90MB. As you are putting the chunks of sound into a MemoryStream (for example) be sure and anticipate if your application hits its memory threshold. Do you need to cache to disk (here’s how)? You are developer, you need to decide. But, in any case, plan for the worst scenario.

Best of luck!


Source: http://blog.jerrynixon.com/2012/02/wp7-sample-microphone-enabled-phone-app.html 


Published at DZone with permission of Jerry Nixon, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

The Windows Phone Microzone, which is supported by Microsoft, is your one-stop-shop for news, tutorials, perspectives, and research on the mobile platform that is making waves in smartphone ecosystem.

Comments

Paul Shezier replied on Fri, 2012/04/13 - 3:03pm

This would be a lot more useful if one could plug-in a microphone into the headphone jack and have audio piped in. Here is why - there is currently no way to stream data from an external device to a windows phone. I say no way because the only conceivable communication channel is if the external device supports wifi and is able to join an existing network. WP7 inexplicably does not support adhoc networks!

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.