Visual Basic Windows Phone 7 Series #2. How to create a microphone application for Windows Phone 7
In our last post, I explained how to create a mini browser for Windows Phone 7. In this blog, I want to share a sample that will help you to create a microphone application for Windows Phone 7. This application will have the feature to capture audio from a microphone and then play back the captured audio. I will now demonstrate how easy it is to create a microphone application for Windows Phone 7, using Visual Basic for Windows Phone Developer Tools.
The microphone application can be created in 4 simple steps as follows:
- Create a sample application and add controls
- Add event handlers
- Build and debug the application
- Rebuild in the release mode before publishing
- Visual Studio 2010 Professional, Premium, or Ultimate. If you do not have any of the releases, you can download any of the following trial versions as per your preference: Professional, Premium, or Ultimate.
- Windows Phone Developer Tools RTW
- Visual Basic
for Windows Phone Developer Tools – RTW
Note: You can install the newer version- Windows Phone Developer Tools 7.1 Beta instead of the above mentioned two tools as it will take comparatively less time. However, this sample was created using the previous version.
To create the microphone application, let’s follow the 4 simple steps mentioned earlier:
Step 1 – Create a sample application and add controls
Create a sample application
- Create a new project and browse to the “Silverlight for Windows Phone” node.
- Select the “Windows Phone Application” template.
- Enter a name for the application.
- Click OK. The MainPage.xaml page is displayed.
- In Solution Explorer, right-click the application name and then select Add Reference.
- Select Microsoft.Xna.Framework from the list, and then click Ok.
- Click the MY APPLICATION text. In the Properties window, change the text property to “SILVERLIGHT MICROPHONE SAMPLE”.
- Click the page name text. In the Properties window, change the text property to “press record”.
- In Solution Explorer, right-click the application name and add a new folder.
- Rename the new folder as “Images”, and add the required icon images to the folder.
- From the toolbox, drag and drop the Image control to the design surface.
- Click the Image control. In the Properties window, change the text property to “StatusImage”.
- Change the source property of the Image control to link it to the icon image you added in the Images folder.
- Add the following XAML code to create the record, play, and stop buttons. This code also links the buttons to the respective icon images from the Images folder.
Your application now looks like this:
Step 2 – Add event handlers
Adding event handlers is one of the important tasks. These event handlers are required to start capturing an audio, stop capturing the audio, and then play back the captured audio.
To add the event handlers:
Open the MainPage.xaml.vb page, and add the following code:
- To simulate the XNA Framework, add the following code to the constructor of your MainPage class:
- To create a new Microphone.BufferReady event handler, add the following code:
After adding all the code mentioned in the previous steps, your constructor should look like the following:
- To implement the BufferReady event handler, add the following code:
Note: While recording, this method copies the data from the microphone to a buffer. Later, it copies that buffer to a stream so that the audio can be played back again.
- Add the following code to create an event handler for the record button.
Note: This method will start capturing the audio. It also allocates memory to hold the audio for 1 second.
- Add the following code to create an event handler for the stop button. This method will stop capturing the audio.
- Add the following code to create an event handler for the play button.
Note: This method will allocate a new SoundEffect object and play back the captured audio.
Voila! Now your microphone for Windows Phone 7 is ready! You just need to build and debug the application.
Step 3 – Build and debug the application
- To build the application, select Build > Build Solution. The project should build without any errors.
- To debug the application, set the deployment target of the application to “Windows Phone 7 Emulator”.
- Select Debug > Start Debugging. The emulator window is displayed.
- To start recording the audio, click the Record button.
- To stop recording the audio, click the Stop button.
- To play back the captured audio, click the Play button.
Note: To stop debugging the application, select Debug > Stop Debugging.
Finally, your microphone application for Windows Phone 7 is ready to be published into the marketplace. Now, you just need to rebuild your application for the release.
Step 4 – Rebuild in the release mode before publishing
- On the standard toolbar, change the configuration manager to Release.
- To rebuild the application, select Build > Rebuild. The XAP file of the application is generated.
To submit your application to the market place, you can refer to upload your application walkthrough.
That’s it! We’ve now seen that creating a microphone application for Windows Phone 7 isn’t that tough. In fact, you’ve created it in just 4 simple steps!
You can find the full source code for the Visual Basic Silverlight Microphone application here.