Visual Basic Windows Phone 7 Series #2. How to create a microphone application for Windows Phone 7

Visual Basic Windows Phone 7 Series #2. How to create a microphone application for Windows Phone 7

Rate This
  • Comments 3

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:

  1. Create a sample application and add controls
  2. Add event handlers
  3. Build and debug the application
  4. Rebuild in the release mode before publishing

Prerequisites:

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

  1. Create a new project and browse to the “Silverlight for Windows Phone” node.
  2. Select the “Windows Phone Application” template.
  3. Enter a name for the application. 
  4. Click OK. The MainPage.xaml page is displayed. 
  5. In Solution Explorer, right-click the application name and then select Add Reference. 
  6. Select Microsoft.Xna.Framework from the list, and then click Ok. 

 

Add controls

  1. Click the MY APPLICATION text. In the Properties window, change the text property to “SILVERLIGHT MICROPHONE SAMPLE”.
  2. Click the page name text. In the Properties window, change the text property to “press record”.
  3. In Solution Explorer, right-click the application name and add a new folder.
  4. Rename the new folder as “Images”, and add the required icon images to the folder.
  5. From the toolbox, drag and drop the Image control to the design surface.
  6. Click the Image control. In the Properties window, change the text property to “StatusImage”.
  7. Change the source property of the Image control to link it to the icon image you added in the Images folder.
  8. 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.

    <phone:PhoneApplicationPage.ApplicationBar>

          <shell:ApplicationBar IsVisible="True" IsMenuEnabled="False">

             <shell:ApplicationBar.Buttons>

                <shell:ApplicationBarIconButton x:Name="recordButton" Text="record" IconUri="/Images/record.png" Click="recordButton_Click" IsEnabled="True"/>

                <shell:ApplicationBarIconButton x:Name="playButton" Text="play" IconUri="/Images/play.png" Click="playButton_Click" IsEnabled="False"/>

                <shell:ApplicationBarIconButton x:Name="stopButton" Text="stop" IconUri="/Images/stop.png" Click="stopButton_Click" IsEnabled="False"/>

             </shell:ApplicationBar.Buttons>

          </shell:ApplicationBar>

    </phone:PhoneApplicationPage.ApplicationBar>

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:

  1. Open the MainPage.xaml.vb page, and add the following code:

    Imports System.IO

    Imports System.Threading

    Imports System.Windows

    Imports System.Windows.Media.Imaging

    Imports System.Windows.Threading

    Imports Microsoft.Phone.Controls

    Imports Microsoft.Phone.Shell

    Imports Microsoft.Xna.Framework

    Imports Microsoft.Xna.Framework.Audio

  2. To simulate the XNA Framework, add the following code to the constructor of your MainPage class:

    ' Timer to simulate the XNA Framework game loop (Microphone is from the XNA Framework).

     

    Dim dt As New DispatcherTimer()

    dt.Interval = TimeSpan.FromMilliseconds(33)

    AddHandler dt.Tick, AddressOf dt_Tick

    dt.Start()

  3. To create a new Microphone.BufferReady event handler, add the following code:

    AddHandler microphone.BufferReady, AddressOf microphone_BufferReady

    After adding all the code mentioned in the previous steps, your constructor should look like the following:

    ''' Constructor

    Public Sub New()

        InitializeComponent()

     

        ' Timer to simulate the XNA Framework game loop (Microphone is from the XNA Framework).

        Dim dt As New DispatcherTimer()

        dt.Interval = TimeSpan.FromMilliseconds(33)

        AddHandler dt.Tick, AddressOf dt_Tick

        dt.Start()

     

        AddHandler microphone.BufferReady, AddressOf microphone_BufferReady

    End Sub

  4. To implement the BufferReady event handler, add the following code:

    Private Sub microphone_BufferReady(ByVal sender As Object, ByVal e As EventArgs)

           

            microphone.GetData(buffer)

     

            stream.Write(buffer, 0, buffer.Length)

    End Sub

    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.

  5. Add the following code to create an event handler for the record button.

    Private Sub recordButton_Click(ByVal sender As Object, ByVal e As EventArgs)

        ' Get audio data in 1 second chunks

        microphone.BufferDuration = TimeSpan.FromMilliseconds(1000)

     

        ' Allocate memory to hold the audio data

        buffer = New Byte(microphone.GetSampleSizeInBytes(microphone.BufferDuration) - 1) {}

     

        ' Set the stream back to zero in case there is already something in it

        stream.SetLength(0)

     

        ' Start recording

        microphone.Start()

     

         SetButtonStates(False, False, True)

        UserHelp.Text = "record"

        StatusImage.Source = microphoneImage

    End Sub

      
    Note: This method will start capturing the audio. It also allocates memory to hold the audio for 1 second. 

  6. Add the following code to create an event handler for the stop button. This method will stop capturing the audio. 
    Private Sub stopButton_Click(ByVal sender As Object, ByVal e As EventArgs)

         If microphone.State = MicrophoneState.Started Then

            ' In RECORD mode, user clicked the

            ' stop button to end recording

            microphone.Stop()

        ElseIf soundInstance.State = SoundState.Playing Then

            ' In PLAY mode, user clicked the

            ' stop button to end playing back

            soundInstance.Stop()

        End If

     

            SetButtonStates(True, True, False)

            UserHelp.Text = "ready"

            StatusImage.Source = blankImage

    End Sub

     
  7. Add the following code to create an event handler for the play button.

    Private Sub playButton_Click(ByVal sender As Object, ByVal e As EventArgs)

            If stream.Length > 0 Then

                ' Update the UI to reflect that

                ' sound is playing

                SetButtonStates(False, False, True)

                UserHelp.Text = "play"

                StatusImage.Source = speakerImage

     

                ' Play the audio in a new thread so the UI can update.

                Dim soundThread As New Thread(New ThreadStart(AddressOf playSound))

                soundThread.Start()

            End If

    End Sub

      
    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

  1. To build the application, select Build > Build Solution. The project should build without any errors.
  2. To debug the application, set the deployment target of the application to “Windows Phone 7 Emulator”. 
  3. Select Debug > Start Debugging. The emulator window is displayed. 
  4. To start recording the audio, click the Record button. 
  5. To stop recording the audio, click the Stop button. 
  6. 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

  1. On the standard toolbar, change the configuration manager to Release
  2. 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.

Summary

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.

Leave a Comment
  • Please add 8 and 1 and type the answer here:
  • Post
  • Great article. And thank you so much for having VB walk-throughs for WP7 - it was the single reason I stayed away from WP7 dev because there used to be no way to do it in VB. Keep the great articles coming!

  • +1 Great to start seeing articles in VB. The C# stuff was ok, but most of my other code is VB. Gotta say, i'm liking the WinPhone 7, though I do wish real multitasking was just around the bend....

  • Thx forr all, but im not a programmer just an user, can you do a ".xap" file for the usersx like me??

Page 1 of 1 (3 items)