博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Windows Phone – Audio recorder
阅读量:4335 次
发布时间:2019-06-07

本文共 3570 字,大约阅读时间需要 11 分钟。

 

 

Audio recorder is a typical application of a mobile phone. Man can use it to record audio from microphone and use it for his ring phone, store audio note or record evidence of crimes as in Hollywood films, etc… Therefore today I decide to write a small audio recorder which should run on any mobile phone using windows phone OS. The application is very small but helpful. You can get source code in the end of this post.

The core of this application is class Microphone of Microsoft.Xna.Framework.Audio which can be used by referencing to Microsoft.Xna.Framework. By declaring a microphone device, setting some predefined features, then I can handle the event BufferReady to record sound section of 1 second from microphone into an array of byte and append it to memory stream.

01 Microphone m_micDevice = Microphone.Default;
02   
03 private void btnStart_Click(object sender, RoutedEventArgs e)
04 {
05     ...
06     m_micDevice.BufferDuration = TimeSpan.FromMilliseconds(1000);
07     m_baBuffer = new byte[m_micDevice.GetSampleSizeInBytes(m_micDevice.BufferDuration)];
08     m_micDevice.BufferReady +=new EventHandler(m_Microphone_BufferReady);
09     m_micDevice.Start();
10 }
11   
12 void m_Microphone_BufferReady(object sender, EventArgs e)
13 {
14     m_micDevice.GetData(m_baBuffer);
15     ...
16     m_msAudio.Write(m_baBuffer,0, m_baBuffer.Length);
17 }

When the users click on Stop button, they will be asked for saving the memory stream to IsolateStorageFile

01 if (txtAudio.Text != "")
02 {
03     IsolatedStorageFile isfData = IsolatedStorageFile.GetUserStoreForApplication();
04     string strSource = txtAudio.Text;
05     int nIndex = 0;
06     while (isfData.FileExists(txtAudio.Text))
07     {
08         strSource = txtAudio.Text + nIndex.ToString().PadLeft(2, '0') + ".wav";
09     }
10   
11     IsolatedStorageFileStream isfStream = new IsolatedStorageFileStream(txtAudio.Text, FileMode.Create, IsolatedStorageFile.GetUserStoreForApplication());
12     isfStream.Write(m_msAudio.ToArray(), 0, m_msAudio.ToArray().Length);
13     isfStream.Close();
14 }

So it’s very simple to write an audio recorder in Windows Phone 7. In my recorder I add some data visualizer to notify the user that the recording is going on. This visualizer uses first 100 value of audio data and shows them in a bar chart. These values will vary continuously and make visualizer animating.

01 <phoneNavigation:PhoneApplicationPage.Resources>
02     <DataTemplate x:Key="template">
03         <StackPanel Orientation="Horizontal" VerticalAlignment="Bottom">
04             <Rectangle Height="{Binding}" Width="5" Fill="Blue" />
05             <Rectangle Width="2" />
06         </StackPanel>
07     </DataTemplate>
08 </phoneNavigation:PhoneApplicationPage.Resources>
09   
10 <ItemsControl x:Name="icBar" ItemsSource="{Binding Path=AudioData}"
11     ItemTemplate="{StaticResource template}" Margin="0,6,0,114">
12     <ItemsControl.ItemsPanel>
13         <ItemsPanelTemplate>
14             <StackPanel Orientation="Horizontal"/>
15         </ItemsPanelTemplate>
16     </ItemsControl.ItemsPanel>
17 </ItemsControl>

As you can see in order to update record progressing on GUI, I used data binding for ItemsSource property of ItemsControl and update this source each time when the event BufferReady is fired.

01 void m_Microphone_BufferReady(object sender, EventArgs e)
02 {
03     ...
04     this.Dispatcher.BeginInvoke(() =>
05         {
06             vm.LoadAudioData(m_baBuffer);
07             ...
08         }
09         );
10     ...
11 }

The complete source code of this audio recorder you can download here ““. If the archive is corrupted, see source code here

http://rongchaua.net/Web/Source/Windows%20Phone%20Audio%20Recorder/

 

转载于:https://www.cnblogs.com/nio-nio/archive/2010/09/18/1830496.html

你可能感兴趣的文章
BFS和DFS的java实现
查看>>
关于jquery中prev()和next()的用法
查看>>
一、 kettle开发、上线常见问题以及防错规范步骤
查看>>
eclipse没有server选项
查看>>
CRC码计算及校验原理的最通俗诠释
查看>>
QTcpSocket的连续发送数据和连续接收数据
查看>>
使用Gitbook来编写你的Api文档
查看>>
jquery扩展 $.fn
查看>>
Markdown指南
查看>>
influxDB的安装和简单使用
查看>>
JPA框架学习
查看>>
JPA、JTA、XA相关索引
查看>>
机器分配
查看>>
php opcode缓存
查看>>
springcloud之Feign、ribbon设置超时时间和重试机制的总结
查看>>
观看杨老师(杨旭)Asp.Net Core MVC入门教程记录
查看>>
UIDynamic(物理仿真)
查看>>
Windows下安装Redis
查看>>
winform非常实用的程序退出方法!!!!!(转自博客园)
查看>>
centos安装vim
查看>>