A group blog from members of the VB team
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:
Prerequisites:
To create the microphone application, let’s follow the 4 simple steps mentioned earlier:
<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>
<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:
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:
Imports System.IOImports System.ThreadingImports System.WindowsImports System.Windows.Media.ImagingImports System.Windows.ThreadingImports Microsoft.Phone.ControlsImports Microsoft.Phone.ShellImports Microsoft.Xna.FrameworkImports Microsoft.Xna.Framework.Audio
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
' 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_Tickdt.Start()
' 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
''' 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_BufferReadyEnd Sub
''' Constructor
Public Sub New()
InitializeComponent()
End Sub
Private Sub microphone_BufferReady(ByVal sender As Object, ByVal e As EventArgs) microphone.GetData(buffer) stream.Write(buffer, 0, buffer.Length)End Sub
Private Sub microphone_BufferReady(ByVal sender As Object, ByVal e As EventArgs)
microphone.GetData(buffer)
stream.Write(buffer, 0, buffer.Length)
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
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 = blankImageEnd Sub
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
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
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()
Voila! Now your microphone for Windows Phone 7 is ready! You just need to build and debug the application.
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.
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.
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??