In this article we will discuss how to reproduce your voice using microphone in Windows Phone 7. Microsoft.Xna.Framework directive has Microphone class which will be used to record the voice using Microphone and play it back.
Let's write code:
Step 1: Place a button inside the contentpanel grid of MainPage.xaml.
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> <Button x:Name="btnRecord" Click="btnRecord_Click" Content="Record" /> </Grid>
Step 2: Add reference of Microsoft.Xna.Framework and add below using directive in MainPage.xaml.cs.
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Audio; using System.IO; using System.Windows.Threading;
Step 3: Define Microphone and below class level objects. data object will be used to capture buffered microphone voice. audio object will be used to write the buffered data.
Microphone mic = Microphone.Default; byte[] data = null; MemoryStream audio = null;
Step 4: BufferReady event handler will be invoked to store the buffered data into MemoryStream object. BufferReady gets triggered on regular interval based on BufferDuration settings.
void mic_BufferReady(object sender, EventArgs e) { mic.GetData(data); audio.Write(data, 0, data.Length); }
Step 5: btnRecord_Click will be responsible for recording and playing of voice and change the content of the button to Stop and Record. As BufferDuration is set to 100 milliseconds, BufferReady event will be triggered in every 100 milliseconds.
private void btnRecord_Click(object sender, RoutedEventArgs e) { if (mic.State == MicrophoneState.Stopped) { mic.BufferDuration = TimeSpan.FromMilliseconds(100); data = new byte[mic.GetSampleSizeInBytes(mic.BufferDuration)]; audio = new MemoryStream(); mic.Start(); this.PageTitle.Text = "Recording..."; btnRecord.Content = "Stop"; } else { mic.Stop(); this.PageTitle.Text = "Playing...."; btnRecord.Content = "Record"; btnRecord.IsEnabled = false; PlayRecordedAudio(); this.PageTitle.Text = "ready"; } }
Step 6: PlayRecordedAudio will be used to play the voice which has been recorded using Microphone. SoundEffect class converts memorystream into array which will played using Play metod.
Play method accepts below parameters.
volume: Volume, ranging from 0.0f (silence) to 1.0f (full volume). 1.0f is full volume relative to SoundEffect.MasterVolume.
pitch: Pitch adjustment, ranging from -1.0f (down one octave) to 1.0f (up one octave). 0.0f is unity (normal) pitch.
pan: Panning, ranging from -1.0f (full left) to 1.0f (full right). 0.0f is centered.
private void PlayRecordedAudio() { SoundEffect se = new SoundEffect(audio.ToArray(), mic.SampleRate, AudioChannels.Stereo); se.Play(1.0f, -1.0f, 0.0f); btnRecord.IsEnabled = true; }
Step 7: Create XNADispatcheService class. FramworkDispatcher processes XNA framework event messages. Update method updates various framework components like power, state and media. IApplicationService interface has definition of two method StartService and StopService. StartService method will be used to Start the DispatcherTime and StopService will be used to Stop the DispatcheTimer. Tick event handler of DispathcerTimer will invoke frameworkDispatcherTimer_Tick periodically to update FrameworkDispatcher.
public class XNADispatcherService : IApplicationService { private DispatcherTimer frameworkDispatcherTimer; public void StartService(ApplicationServiceContext context) { this.frameworkDispatcherTimer.Start(); }
public void StopService() { this.frameworkDispatcherTimer.Stop(); }
public XNADispatcherService() { this.frameworkDispatcherTimer = new DispatcherTimer(); this.frameworkDispatcherTimer.Interval = TimeSpan.FromTicks(333333); this.frameworkDispatcherTimer.Tick += frameworkDispatcherTimer_Tick; FrameworkDispatcher.Update(); }
void frameworkDispatcherTimer_Tick(object sender, EventArgs e) { FrameworkDispatcher.Update(); } }
Step 8: We need to the namespace and objects marked in bold in App.xaml
< Application x:Class="Windows_Phone_7___Microphone_Repeater.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone" xmlns:local="clr-namespace:Windows_Phone_7___Microphone_Repeater"><!-- Namespace of the application-->
<!--Application Resources--> <Application.Resources> </Application.Resources>
<Application.ApplicationLifetimeObjects> <!--Required object that handles lifetime events for the application--> <shell:PhoneApplicationService Launching="Application_Launching" Closing="Application_Closing" Activated="Application_Activated" Deactivated="Application_Deactivated"/> <local:XNADispatcherService/> </Application.ApplicationLifetimeObjects> </Application>
Step 9: Run the application, press Record button and speak, press Stop button to repeat it back by Windows Phone 7.

This ends the article of Windows Phone 7 microphone repeater.
|