Voice Recording/Mic Testing and Playing Recorded Sound in Windows Phone Silverlight 8.1

Mainpage.Xaml

  1. <phone:PhoneApplicationPage x:Class="MicrophoneTest.MainPage" 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:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" FontFamily="{StaticResource PhoneFontFamilyNormal}" FontSize="{StaticResource PhoneFontSizeNormal}" Foreground="{StaticResource PhoneForegroundBrush}" SupportedOrientations="Portrait" Orientation="Portrait" shell:SystemTray.IsVisible="True">  
  2.     <!--LayoutRoot is the root grid where all page content is placed-->  
  3.     <Grid x:Name="LayoutRoot" Background="LightBlue">  
  4.         <Image x:Name="ScanComp" HorizontalAlignment="Left" Height="170" Margin="156,235,0,0" VerticalAlignment="Top" Width="170" />  
  5.         <TextBlock Name="timerMsg" Margin="10,460,10,257" TextAlignment="Center" Foreground="White" FontSize="24"></TextBlock>  
  6.         <TextBlock Name="timedisplayBlock" Margin="206,508,221,209" Foreground="White" FontSize="40"></TextBlock>  
  7.         <Button Name="btnstart" Content="start" Click="recordButton_Click" Margin="0,636,0,58"></Button>  
  8.     </Grid>  
  9. </phone:PhoneApplicationPage>  

Mainpage.xaml.cs 

  1. public partial class MainPage: PhoneApplicationPage  
  2. {  
  3.     // Constructor  
  4.     int currentcount;  
  5.     Microphone microphone = Microphone.Default;  
  6.     byte[] buffer;  
  7.     MemoryStream stream = new MemoryStream();  
  8.     SoundEffect sound;  
  9.     DispatcherTimer recordingTimer;  
  10.     public MainPage()  
  11.     {  
  12.         InitializeComponent();  
  13.         recordingTimer = new DispatcherTimer();  
  14.         recordingTimer.Interval = new TimeSpan(0, 0, 0, 1, 0);  
  15.         recordingTimer.Tick += new EventHandler(recordingTimer_Tick);  
  16.     }  
  17.     private void recordButton_Click(object sender, EventArgs e)  
  18.     {  
  19.         Startrecording();  
  20.     }  
  21.     protected async void Startrecording()  
  22.     {  
  23.         ScanComp.Source = new BitmapImage(new Uri(@ "Assets/scan_microphone.png", UriKind.Relative));  
  24.         timerMsg.Text = "Speak something to test recording.";  
  25.         currentcount = 10;  
  26.         recordingTimer.Start();  
  27.         DispatcherTimer dt = new DispatcherTimer();  
  28.         dt.Interval = TimeSpan.FromMilliseconds(33);  
  29.         dt.Tick += delegate  
  30.         {  
  31.             try  
  32.             {  
  33.                 FrameworkDispatcher.Update();  
  34.             }  
  35.             catch  
  36.             {}  
  37.         };  
  38.         dt.Start();  
  39.         microphone.BufferReady += new EventHandler < EventArgs > (microphone_BufferReady);  
  40.         microphone.BufferDuration = TimeSpan.FromMilliseconds(1000);  
  41.         buffer = new byte[microphone.GetSampleSizeInBytes(microphone.BufferDuration)];  
  42.         microphone.Start();  
  43.         await Task.Delay(TimeSpan.FromSeconds(10));  
  44.         if (microphone.State == MicrophoneState.Started)  
  45.         {  
  46.             microphone.Stop();  
  47.         }  
  48.         timerMsg.Text = "playing sound...";  
  49.         sound = new SoundEffect(stream.ToArray(), microphone.SampleRate, AudioChannels.Mono);  
  50.         sound.Play();  
  51.     }  
  52.     void microphone_BufferReady(object sender, EventArgs e)  
  53.     {  
  54.         microphone.GetData(buffer);  
  55.         stream.Write(buffer, 0, buffer.Length);  
  56.     }  
  57.     private void recordingTimer_Tick(object sender, EventArgs e)  
  58.     {  
  59.         timedisplayBlock.Text = currentcount--.ToString();  
  60.         if (currentcount == 0)  
  61.         {  
  62.             recordingTimer.Stop();  
  63.             timedisplayBlock.Text = "";  
  64.             timerMsg.Text = "playing sound...";  
  65.         }  
  66.     }  
  67. }