windows phone开发学习--录音

来源:互联网 发布:flac分轨软件 编辑:程序博客网 时间:2024/06/10 18:42

http://blog.csdn.net/jj12345jj198999/article/details/8512401

windows phone开发学习--录音       

        分类:            Felven在职场windows phone学习113人阅读评论(0)收藏举报

目录(?)[+]

  1. 确定特征功能
  2. 使用Silverlight应用中的XNA类
  3. 使用Microphone类录音
  4. 录音回放
  5. 跟踪录音
  6. 保存状态和墓碑化

在windows phone的开发中,有时候我们需要在程序中嵌入一段语音,至少这要比打字速度快上很多。之前在android和ios的市场上上已经发现了这种集成录音功能的应用,貌似那两个系统都提供了接口,我想在windows phone肯定也能做到这一点。遗憾的是,在国内网站上面搜索时找到的资料很少,当我按英文检索时立刻就发现了一篇很有用的资料,Making a Voice Recorder on Windows Phone 于是下面就是对这篇文章的粗略翻译(略去了我认为不重要的东西)。

确定特征功能

在开始写应用之前,我想好了这个程序要实现哪些功能,列表如下:

  • 导出录音
  • 用WAV格式保存录音
  • 为录音添加备注
  • 加快或放慢录音速度
  • 改变语音
  • 混合,分割,编辑录音
  • 以MP3格式导出
  • 分类标记
  • 时间/日期提醒
你可以看到,在一个录音程序应用之中可以添加进很多不同的东西,应用也就很快由从简单变得很复杂。为了不让应用变得过于复杂,我选择了一个能实现我主要目地的最小功能集合,由于比较简单这样也避免了在很多地方引入潜在的BUG,缩减的功能集合如下:
  • 以WAV格式保存录音
  • 按照日期或名字对录音排序
  • 在屏幕锁定时进行录音
  • 为录音添加备注

使用Silverlight应用中的XNA类


你可以在windows phone上面创建两种类型的应用,一种是使用Silverlight作为UI的应用,另一种是使用XNA渲染类作为UI的应用。开发时两者只能选其一。Silverlight提供了一些控件能够让你在创建应用UI的时候使用,例如button, textbox,label等等;而在XNA中,你需要创建你自己需要的一切来达到你想要展示出来的效果。基于以上这些特点,最终在该应用中我采用了Silverlight UI。
为了录音,我必须使用从Microsoft.Xna.Framework.Audio派生出来的Microphone类。尽管我们不能再Silverlight应用中使用XNA渲染类,我们仍然可以使用很多其他的XNA类。使用音频相关的XNA类需要间断性的调用FrameworkDispatcher.Update()。为了避免使用计时器调用函数时程序逻辑的循环,你可要使用微软提供的一个ApplicationService例子,该例子可以实现相同的功能。这个类将会很好的为你调用这个函数,整个类如下:
[csharp] view plaincopyprint?
  1. public class XNAFrameworkDispatcherService : IApplicationService 
  2.     private DispatcherTimer frameworkDispatcherTimer; 
  3.  
  4.     public XNAFrameworkDispatcherService() 
  5.     { 
  6.         this.frameworkDispatcherTimer =new DispatcherTimer(); 
  7.         this.frameworkDispatcherTimer.Interval = TimeSpan.FromTicks(333333); 
  8.         this.frameworkDispatcherTimer.Tick += frameworkDispatcherTimer_Tick; 
  9.         FrameworkDispatcher.Update(); 
  10.     } 
  11.  
  12.     void frameworkDispatcherTimer_Tick(object sender, EventArgs e) 
  13.          { FrameworkDispatcher.Update(); } 
  14.  
  15.     void IApplicationService.StartService(ApplicationServiceContext context) 
  16.          { this.frameworkDispatcherTimer.Start(); } 
  17.  
  18.     void IApplicationService.StopService() {this.frameworkDispatcherTimer.Stop(); } 

一旦这个类在你的工程中被声明,它需要被作为应用生命期的对象添加进来。这里有很多方法实现这一点,但是我喜欢把他添加到App.xaml之中。
[html] view plaincopyprint?
  1. <Application.ApplicationLifetimeObjects> 
  2.     <!--Required object that handles lifetime events for the application--> 
  3.     <shell:PhoneApplicationService  
  4.         Launching="Application_Launching"Closing="Application_Closing"  
  5.         Activated="Application_Activated"Deactivated="Application_Deactivated"/> 
  6.     <local:XNAFrameworkDispatcherService/>         
  7. </Application.ApplicationLifetimeObjects> 

完成了这一步,我不再需要再考虑FrameworkDispatcher.Update.它会在程序启动时自动执行并在程序关闭时自动终止。

使用Microphone类录音


Microphone类把录音记录在组块中,然后在记录一个新组块时把前一组块传递到你的程序中。为了做到这一点,Microphone类拥有它自己的内存缓冲区。例如当你要记录一个短句:"The quick brown fox jumped over the lazy dog."现在我们同时假定麦克风的缓冲区去一次能够记录一个单词。


图1:麦克风,缓冲区,你的程序

你开始说这个短句,麦克风缓冲区填满了你说出单词"The"的声音。


一旦缓冲区满了,它就被传送到程序中,同时麦克风开始用下一个记录的单词填充一个新的缓冲区。程序接收到缓冲区并能够对其进行处理。考虑到程序是为了保存和播放录音,程序将会保存音频组块并且等待下一个组块以便拼接在前一个的后面。(注:这里buffer翻译为缓冲区,不过感觉有点别扭,将就看了)

图3: 程序接收到第一个单词,同时第二个单词被记录

当每一个组块被记录后,它会被传递到程序中,程序把该组块拼接在它已经收到的组块之后。不过当用户说完最后一个单词时,这里会产生BUG


在很多网上的例子中,当用户说完最后一个单词dog并且按下停止按钮时,程序停止接收从麦克风传过来的更多信息,但最后一个单词还没有从麦克风的缓冲区传递到程序缓冲区中。最终的结果就是程序接收到了除了最后一个单词以外的全部内容。为了防止这个问题,需要考虑当用户停止录音时该发生什么,程序应该等待直到它接收到停止前的最后一个缓冲区而不是立即停止。在最坏的情况下,句子结尾后的一些杂音可能也会被记录下来,但这也好过丢失数据。我们可以通过减少缓冲区的大小来减少接收到多余的数据量。

创建实现上面功能的代码还是相对简单的。为了获取一个Microphone类的实例,我们可以从Microphone.Current获取。当麦克风在录音时,通过产生一个BufferReady事件,它会告知我们的程序一个缓冲区可以读取了。随后我们可以使用GetBuffer(byte[] destination)获取缓冲区的数据。在这个方法中,我们需要传递一个用于接收数据的字节数组。这个数组大小如何设定?Microphone类拥有两个成员来帮助我们确定需要的大小。Microphone.BufferDuration让我们知道麦克风的缓冲区能存储多少时间的录音,Microphone.GetSampleSizeInBytes(Timespan)方法告诉我们记录一个特定长度的录音需要多少字节。把两者结合在一起,我们需要的缓冲区大小就是Microphone.GetSampleSizeInBytes(Microphone.BufferDuration).一旦你拥有了Microphone类的一个实例,把其关联到BufferReady事件中,创建好了接收数据的缓冲区,录音过程可以通过调用Microphone.Start()开始。

在BufferReady处理事件程序中需要做一些操作。当数据从缓冲区取出时,需要在某个地方把它们收集起来。当这些数据被收集后,我们需要检查是否存在一个停止录音的请求。如果有请求,就告诉Microphone实例在使用Microphone.Stop()之前停止发送数据,并且进行保持录音的操作。为了收集数据,我将使用一个memory stream在录音结束时把它写入到隔离存储区中。我的一个要求是使用WAV格式存储录音数据,这可以通过在写完所有接收到的字节前先写一个合适的波头实现。(参看:writing a proper wave header)。下面是完成上面操作的代码:

[csharp] view plaincopyprint?
  1. <span style="font-family: Arial, Helvetica, sans-serif;">//code for recording from the microphone and saving to a file 
  2. public void StartRecording() 
  3.     if (_currentMicrophone == null
  4.     { 
  5.         _currentMicrophone = Microphone.Default; 
  6.         _currentMicrophone.BufferReady +=  
  7.            new EventHandler<EventArgs>(_currentMicrophone_BufferReady); 
  8.         _audioBuffer = newbyte[_currentMicrophone.GetSampleSizeInBytes( 
  9.                             _currentMicrophone.BufferDuration)]; 
  10.         _sampleRate = _currentMicrophone.SampleRate; 
  11.     } 
  12.     _stopRequested = false
  13.     _currentRecordingStream = new MemoryStream(1048576); 
  14.     _currentMicrophone.Start(); 
  15.  
  16. public void RequestStopRecording() 
  17.     _stopRequested = true
  18.  
  19. void _currentMicrophone_BufferReady(object sender, EventArgs e) 
  20.     _currentMicrophone.GetData(_audioBuffer); 
  21.     _currentRecordingStream.Write(_audioBuffer,0,_audioBuffer.Length); 
  22.     if (!_stopRequested)  
  23.         return
  24.     _currentMicrophone.Stop(); 
  25.  
  26.     var isoStore =  
  27.       System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication(); 
  28.  
  29.     using (var targetFile = isoStore.CreateFile(FileName)) 
  30.     { 
  31.         WaveHeaderWriter.WriteHeader(targetFile,  
  32.               (int)_currentRecordingStream.Length, 1, _sampleRate); 
  33.         var dataBuffer = _currentRecordingStream.GetBuffer(); 
  34.         targetFile.Write(dataBuffer,0,(int)_currentRecordingStream.Length); 
  35.         targetFile.Flush(); 
  36.         targetFile.Close(); 
  37.     } 
  38. }</span> 


录音回放


为了回放录音,我将使用SoundEffect类。类似于Microphone类,SoundEffect也是一个XNA音频类需要定期调用FrameworkDispatcher.Update()函数。有两种方法可以让我载入WAVE文件。我可以自己对波头进行解码,或者让SoundEffect类来做,这里我给出的自己解码的过程,这样其他人可以对文件进行修改。(注:并没有发现作者给出的自己写的参考)

当通过构造函数初始化SoundEffect时,需要知道三个数据:录音的音频数据,采样率,记录音轨的数量。这个应用记录的只是普通音而非立体声。所以这里一直都只有一个音轨。我可以传递AudioChannels.Mono作为参数。但在以后,我会加入导入录音的功能(录音可能是立体声),所以我把这个数据从波头上取下。类似的,我也可以从Microphone类中获取采样率而不是从波头中获取。但为了以后着想,我还是从波头获取数据,波头之后跟着的就是波数据。一旦SoundEffect被初始化,为了播放录音,我需要获取一个SoundEffectInstance实例然后调用Play方法。

我不认为我需要解释为什么一次只播放一个录音,所以在播放一个新的录音片段前,我先检查内存中是否已经存在一个片段,如果存在就将其终止。

[csharp] view plaincopyprint?
  1. public void PlayRecording(RecordingDetails source) 
  2.     if(_currentSound!=null
  3.     { 
  4.         _currentSound.Stop(); 
  5.         _currentSound = null
  6.     } 
  7.     var isoStore = System.IO.IsolatedStorage.IsolatedStorageFile. 
  8.                                  GetUserStoreForApplication(); 
  9.     if(isoStore.FileExists(source.FilePath)) 
  10.     { 
  11.         byte[] fileContents; 
  12.         using (var fileStream = isoStore.OpenFile(source.FilePath, FileMode.Open)) 
  13.         { 
  14.             fileContents = newbyte[(int) fileStream.Length]; 
  15.             fileStream.Read(fileContents, 0, fileContents.Length); 
  16.             fileStream.Close();//not really needed, but it makes me feel better. 
  17.         } 
  18.           
  19.         int sampleRate =((fileContents[24] <<  0) | (fileContents[25] <<  8) |  
  20.                          (fileContents[26] << 16) | (fileContents[27] << 24)); 
  21.  
  22.         AudioChannels channels = (fileContents[22] == 1) ?  
  23.                         AudioChannels.Mono : AudioChannels.Stereo; 
  24.  
  25.         var se = new SoundEffect(fileContents, 44,  
  26.             fileContents.Length - 44, sampleRate, channels, 0, 
  27.                                     0); 
  28.         _currentSound = se.CreateInstance(); 
  29.         _currentSound.Play(); 
  30.     } 


通过SoundEffect.FromFile载入声音是简单直接的

[csharp] view plaincopyprint?
  1. public void PlayRecording(RecordingDetails source) 
  2.     SoundEffect se; 
  3.     if(_currentSound!=null
  4.     { 
  5.         _currentSound.Stop(); 
  6.         _currentSound = null
  7.     } 
  8.     var isoStore = System.IO.IsolatedStorage. 
  9.                      IsolatedStorageFile.GetUserStoreForApplication(); 
  10.     if(isoStore.FileExists(source.FilePath)) 
  11.     { 
  12.         byte[] fileContents; 
  13.         using (var fileStream = isoStore.OpenFile(source.FilePath, FileMode.Open)) 
  14.         { 
  15.             se = SoundEffect.FromStream(fileStream); 
  16.             fileStream.Close();//not really needed, but it makes me feel better. 
  17.         } 
  18.  
  19.         _currentSound = se.CreateInstance(); 
  20.         _currentSound.Play(); 
  21.     } 

跟踪录音


除了把录音记录在隔离存储之中外,我想跟踪一些其他的信息,例如录音创建的日期,录音标题,录音备注等等。通过给文件命名或者从文件数据中推断录制日期从而给录音一个标题是可能的,但这种解决方法并不持久。在对文件命名时存在一些字符的限制,并且在以后当我增加导入或导出文件时,可能存在文件日期丢失的情况。因此我创建了一个类用来保存我想要在录音上跟踪的信息。类如下所示:
[csharp] view plaincopyprint?
  1. public class RecordingDetails 
  2.    public string    Title {get; set; } 
  3.    public string    Details {get; set; } 
  4.    public DateTime  TimeStamp {get; set; } 
  5.    public string    FilePath {get; set; } 
  6.    public string    SourcePath {get; set; } 

为了让这个类易读,这里我给了一个简化的形式。这个类需要被序列化以便我可以从隔离存储中进行读写。所以这个类被标记上[DateContract]属性,其成员被标记上[DateMember]属性。我计划把这个类的实例绑到UI元素中,所以这个类需以用INotifyPropertyChanged接口进行实现。该类的版本如下:

[csharp] view plaincopyprint?
  1. [DataContract] 
  2. public class RecordingDetails: INotifyPropertyChanged  
  3.                  
  4.     // Title - generated from ObservableField snippet - Joel Ivory Johnson 
  5.  
  6.     private string _title; 
  7.     [DataMember] 
  8.     public string Title 
  9.     { 
  10.     get { return _title; } 
  11.         set 
  12.         { 
  13.             if (_title != value) 
  14.             { 
  15.                 _title = value; 
  16.                 OnPropertyChanged("Title"); 
  17.             } 
  18.         } 
  19.     } 
  20.     //----- 
  21.  
  22.                  
  23.     // Details - generated from ObservableField snippet - Joel Ivory Johnson 
  24.  
  25.     private string _details; 
  26.     [DataMember] 
  27.     public string Details 
  28.     { 
  29.     get { return _details; } 
  30.         set 
  31.         { 
  32.             if (_details != value) 
  33.             { 
  34.                 _details = value; 
  35.                 OnPropertyChanged("Details"); 
  36.             } 
  37.         } 
  38.     } 
  39.     //----- 
  40.  
  41.                  
  42.     // FilePath - generated from ObservableField snippet - Joel Ivory Johnson 
  43.  
  44.     private string _filePath; 
  45.     [DataMember] 
  46.     public string FilePath 
  47.     { 
  48.     get { return _filePath; } 
  49.         set 
  50.         { 
  51.             if (_filePath != value) 
  52.             { 
  53.                 _filePath = value; 
  54.                 OnPropertyChanged("FilePath"); 
  55.             } 
  56.         } 
  57.     } 
  58.     //----- 
  59.  
  60.                  
  61.     // TimeStamp - generated from ObservableField snippet - Joel Ivory Johnson 
  62.  
  63.     private DateTime _timeStamp; 
  64.     [DataMember] 
  65.     public DateTime TimeStamp 
  66.     { 
  67.     get { return _timeStamp; } 
  68.         set 
  69.         { 
  70.             if (_timeStamp != value) 
  71.             { 
  72.                 _timeStamp = value; 
  73.                 OnPropertyChanged("TimeStamp"); 
  74.             } 
  75.         } 
  76.     } 
  77.     //----- 
  78.  
  79.                  
  80.     // SourceFileName - generated from ObservableField snippet - Joel Ivory Johnson 
  81.  
  82.     private string _sourceFileName; 
  83.     [IgnoreDataMember] 
  84.     public string SourceFileName 
  85.     { 
  86.     get { return _sourceFileName; } 
  87.         set 
  88.         { 
  89.             if (_sourceFileName != value) 
  90.             { 
  91.                 _sourceFileName = value; 
  92.                 OnPropertyChanged("SourceFileName"); 
  93.             } 
  94.         } 
  95.     } 
  96.     //----- 
  97.  
  98.                  
  99.     // IsNew - generated from ObservableField snippet - Joel Ivory Johnson 
  100.  
  101.     private bool _isNew =false
  102.     [IgnoreDataMember] 
  103.     public bool IsNew 
  104.     { 
  105.     get { return _isNew; } 
  106.         set 
  107.         { 
  108.             if (_isNew != value) 
  109.             { 
  110.                 _isNew = value; 
  111.                 OnPropertyChanged("IsNew"); 
  112.             } 
  113.         } 
  114.     } 
  115.     //----- 
  116.  
  117.                  
  118.     // IsDirty - generated from ObservableField snippet - Joel Ivory Johnson 
  119.  
  120.     private bool _isDirty =false
  121.     [IgnoreDataMember] 
  122.     public bool IsDirty 
  123.     { 
  124.     get { return _isDirty; } 
  125.         set 
  126.         { 
  127.             if (_isDirty != value) 
  128.             { 
  129.                 _isDirty = value; 
  130.                 OnPropertyChanged("IsDirty"); 
  131.             } 
  132.         } 
  133.     } 
  134.     //----- 
  135.  
  136.  
  137.     public void Copy(RecordingDetails source) 
  138.     { 
  139.         this.Details = source.Details; 
  140.         this.FilePath = source.FilePath; 
  141.         this.SourceFileName = source.SourceFileName; 
  142.         this.TimeStamp = source.TimeStamp; 
  143.         this.Title = source.Title; 
  144.     } 
  145.  
  146.     public event PropertyChangedEventHandler PropertyChanged; 
  147.     protected void OnPropertyChanged(string propertyName) 
  148.     { 
  149.         if (PropertyChanged != null
  150.         { 
  151.             PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
  152.         } 
  153.     } 
  154.  


[DataMember]属性贯穿了整个代码,所以我可以使用数据合同序列化读写该类。由于我使用了DataContractSerializer,我并不需要过于操心当文件被存储和载入时具体的编码细节。同时使用隔离存储不是很难,我使用了之前我自己写的一个实用类的变种(具体看这里)来把序列化和反序列化的简化为一小段代码。当用户创建一个新的录音时,该类的一个新的实例也会被创建。除了标题,备注和时间戳外,这个类也包含描述该录音的路径,一个指出数据从哪加载的原始文件名称的非序列化成员SourceFileName。如果没有这些信息,当用户决定更新数据时,便无法知道当保存内容时哪个文件需要被重写。

[csharp] view plaincopyprint?
  1. //Saving Data 
  2. var myDataSaver = new DataSaver<RecordingDetails>() {}; 
  3. myDataSaver.SaveMyData(LastSelectedRecording,  
  4.                        LastSelectedRecording.SourceFileName); 
  5.  
  6. //Loading Data 
  7. var myDataSaver = new DataSaver<RecordingDetails>(); 
  8. var item = myDataSaver.LoadMyData(LastSelectedRecording.SourceFileName); 


这样操作后,你就拥有了进行录音,保存录音,载入录音的全部信息,当程序第一次启动时,我让其载入所有的RecordingDetails并且把他们加载到我的视图模式中的一个ObservableCollection中。在那里它们可以以一种列表的形式展现给用户。

[csharp] view plaincopyprint?
  1. public void LoadData() 
  2.     var isoStore =  
  3.         System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication(); 
  4.     var recordingList = isoStore.GetFileNames("data/*.xml"); 
  5.     var myDataSaver = new DataSaver<RecordingDetails>(); 
  6.     Items.Clear(); 
  7.     foreach (var desc in recordingList.Select(item => 
  8.                     { 
  9.                         var result =myDataSaver.LoadMyData(String.Format("data/{0}", item)); 
  10.                         result.SourceFileName = String.Format("data/{0}", item); 
  11.                         return result; 
  12.                    })) 
  13.     { 
  14.         Items.Add(desc); 
  15.     } 
  16.     this.IsDataLoaded = true

保存状态和墓碑化

你的程序可以在任何时间被中断,例如一个电话,一个突然跳出程序进行的搜索等等。当这一切发生时,你的应用会被墓碑化;操作系统将会保存用户所在的页面并且给程序保存其他数据的机会。当程序被再次载入时,开发者必须确保采取适当步骤重新加载状态。大多数情况下,我并不需要担心墓碑化因为程序大部分状态数据被迅速保存到隔离存储中。这里也没有多少需要保存的状态数据,因为录音随着程序设置的改变会被立即执行。


下面内容不再翻译,无非是作者提出自己想增加应用功能的说明。最后给出源码下载地址和应用截图:



下载地址:http://www.codeproject.com/KB/windows-phone-7/WpVoiceMemo/WpVoiceMemo.zip

 

原创粉丝点击