Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
All right, so we are going to build the smallest application that we can build that interacts with the Xna Framework and capture sound. Let's take a quick look at the solution. First we added the Xna.Framework and Xna.Framework.GamerServices, Library references so that we can access the Xna.Framework. Then we added a couple of images from the SDK icon library, one of a Microphone and one of a Square or a Stop button so that we can have our button at the bottom of the screen in the Application Bar, toggle back and forth between Record and Stop.
And then finally we've built some code. In our code we have System.Windows. Threading, because we need to create a dispatch timer to create a periodic callback in order to drive the Xna.Framework, and then we have the two Xna.Framework usings so that we can get to the basic framework, and we can also get to the audio which is where the Microphone code is located. In terms of instance variables for our main page here, we have a Microphone object, which is how we're going to ask the system to start feeding us data. We assigned it to the Microphone Default property.
Currently, no Windows Phone devices have more than one Microphone, but in the future it might be possible to differentiate between the hardware Microphone on the device and a microphone on a handset. And we have a buffer in which we are going to put the samples returned to us by the Xna.Framework and then a MemoryStream where we are going to write the buffers out as each buffer comes in. We write it out to the stream so that when we are done we could then save that stream to a file, which we will be doing in a future chapter. And then finally, we have a boolean here inRecordingMode, which we'll use to determine what we should do when someone taps the button on the Application Bar.
If we are not inRecordingMode, we start recording, if we are inRecordingMode, we stop. As I mentioned, in order to use code from the Xna.Framework, we need to essentially pull the Xna.Framework by calling this method FrameworkDispatcher.Update, and this code here in the middle of the screen right now simply creates a timer that fires off every 50 milliseconds and calls Framework.Dispatcher.Update so that we can get frequent updates of data from the Microphone. And finally, in order to start we need to fire it off just once in order to be able to get things going.
When we are ready to figure out how big of a buffer of the data we want, we tell the Microphone how much data we want per callback, in this case we are saying a half a second worth of data, which is good for our first application because we can see the data coming in at a slow enough rate that we can manage. In one of the later recordings in this chapter, we're actually going to cut that way down so that we can actually display some visual feedback which we need to do a lot more quickly than every half a second. And finally, we tell the Microphone, here is your BufferReady handler, here is our EventHandler for when Microphone data is ready, call us back there.
We also have a MainPage_Loaded Event Handler that sets one of the textboxes on our user interface to say Tap record to begin when the page is loaded. Once we start recording, the Microphone code in the Xna.Framework will fire its BufferReady event which we attached to this microphone_BufferReady method and give us a buffer full of data each time. So we write out how much data we got, we can write that stream to the buffer, and then we figure out what position we are within that recording stream and set a text block on our screen so that we can show the user 1 second, 2 second, 3 seconds of recording.
And finally, we have the code that handles our Record button, as we mentioned before, if we are inRecordingMode, we stop, change the text for the button, back to record, set the image for the button, back to the microphone, and set the textbox to say Recording Complete, and of course we set inRecordingMode = False. If we are not recording, we create a new MemorySstream, we go allocate the buffer by asking the Microphone to give it its sample size in bytes based on the duration that we told it. So we said to it we want to be called every half a second.
It's then going to compute how many bytes that is so that we can allocate our buffer, and we change the text of the button to stop, and we change the image to the appbar.stop.rest.png. And Finally, we call microphone.Start which starts the process of the Microphone calling us back, giving its buffers full data, and we set inRecordingMode to true. So let's give this a try. So we have a single portrait page template that we have started with, we set the name to record & play.
We have a text box here where we are going to put the time, and we have our App. bar down here with a Recording button. So we touch the Recording button, and we are now recording. You can see the time is updating, and you can also see a debug output saying we have received 16,000 bytes and then other 16,000 bytes and then other 16,000 bytes. And you can see if you are looking at a large enough screen that the size of the scrollbar thumb here is getting smaller and smaller because this is continuing on, as we get more and more of these lines in the Debug output. So, now we know we have got the basic Recording mechanism going, we are getting data back from the recording, and let's move on to the next part where we will actually take a look at that data and provides some visual feedback of the data actually coming in.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 104258 Viewers
56 Video lessons · 116197 Viewers
71 Video lessons · 85407 Viewers
131 Video lessons · 40894 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.