<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">
- <!--LayoutRoot is the root grid where all page content is placed-->
- <Grid x:Name="LayoutRoot" Background="LightBlue">
- <Image x:Name="ScanComp" HorizontalAlignment="Left" Height="170" Margin="156,235,0,0" VerticalAlignment="Top" Width="170" />
- <TextBlock Name="timerMsg" Margin="10,460,10,257" TextAlignment="Center" Foreground="White" FontSize="24"></TextBlock>
- <TextBlock Name="timedisplayBlock" Margin="206,508,221,209" Foreground="White" FontSize="40"></TextBlock>
- <Button Name="btnstart" Content="start" Click="recordButton_Click" Margin="0,636,0,58"></Button>
- </Grid>
- </phone:PhoneApplicationPage>
- public partial class MainPage: PhoneApplicationPage
- {
- // Constructor
- int currentcount;
- Microphone microphone = Microphone.Default;
- byte[] buffer;
- MemoryStream stream = new MemoryStream();
- SoundEffect sound;
- DispatcherTimer recordingTimer;
- public MainPage()
- {
- InitializeComponent();
- recordingTimer = new DispatcherTimer();
- recordingTimer.Interval = new TimeSpan(0, 0, 0, 1, 0);
- recordingTimer.Tick += new EventHandler(recordingTimer_Tick);
- }
- private void recordButton_Click(object sender, EventArgs e)
- {
- Startrecording();
- }
- protected async void Startrecording()
- {
- ScanComp.Source = new BitmapImage(new Uri(@ "Assets/scan_microphone.png", UriKind.Relative));
- timerMsg.Text = "Speak something to test recording.";
- currentcount = 10;
- recordingTimer.Start();
- DispatcherTimer dt = new DispatcherTimer();
- dt.Interval = TimeSpan.FromMilliseconds(33);
- dt.Tick += delegate
- {
- try
- {
- FrameworkDispatcher.Update();
- }
- catch
- {}
- };
- dt.Start();
- microphone.BufferReady += new EventHandler < EventArgs > (microphone_BufferReady);
- microphone.BufferDuration = TimeSpan.FromMilliseconds(1000);
- buffer = new byte[microphone.GetSampleSizeInBytes(microphone.BufferDuration)];
- microphone.Start();
- await Task.Delay(TimeSpan.FromSeconds(10));
- if (microphone.State == MicrophoneState.Started)
- {
- microphone.Stop();
- }
- timerMsg.Text = "playing sound...";
- sound = new SoundEffect(stream.ToArray(), microphone.SampleRate, AudioChannels.Mono);
- sound.Play();
- }
- void microphone_BufferReady(object sender, EventArgs e)
- {
- microphone.GetData(buffer);
- stream.Write(buffer, 0, buffer.Length);
- }
- private void recordingTimer_Tick(object sender, EventArgs e)
- {
- timedisplayBlock.Text = currentcount--.ToString();
- if (currentcount == 0)
- {
- recordingTimer.Stop();
- timedisplayBlock.Text = "";
- timerMsg.Text = "playing sound...";
- }
- }
- }
No comments:
Post a Comment