2014-03-06 142 views
3

我将在Windows Phone 7应用程序中研究录音功能。 我通过此参考link实现了录制功能。windows phone 7录音问题

它完全正常工作,在我的情况也是如此。

实际情况是,在我的应用程序中,我创建了第一页,它将作为与上述引用链接相同的录制屏幕。 ,当我们停止录制时,我重定向到第二页,并将该录制内容保存在隔离存储中,并在第二页上绑定录制的声音。在这里,我播放录制的声音,它工作正常。

现在,当我再次进入录制屏幕(第一页)并开始另一个录制。它会有一些时间记录良好,有些时候会在录制过程中跳过一些声音,就像哔声一样,它会在录制时看起来像一个额外的噪音,并没有得到正确的录音声音。

我的代码是什么样子,

public partial class NikhilRecord : PhoneApplicationPage 
{ 
    //XNA Objects for Record And Playback 
    Microphone mphone; 

    //Used for Storing captured buffers 
    List<byte[]> memobuffercollection = new List<byte[]>(); 

    //Used for displaying stored memos 
    ObservableCollection<MemoInfo> memofiles = new ObservableCollection<MemoInfo>(); 

    SpaceTime spaceTime = new SpaceTime(); 

    public NikhilRecord() 
    { 
     InitializeComponent(); 

     //Create new Microphone and set event handler. 
     mphone = Microphone.Default; 
     mphone.BufferReady += OnMicrophoneBufferReady; 
     String FileName = PhoneApplicationService.Current.State["MySelectedSong"].ToString(); 

     using (IsolatedStorageFile IsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication()) 
     { 
      try 
      { 
       using (IsolatedStorageFileStream fileStream = IsolatedStorage.OpenFile(FileName, FileMode.Open, FileAccess.Read)) 
       { 
        MyMedia.SetSource(fileStream);      
        MyMedia.CurrentStateChanged += new RoutedEventHandler(mediaPlayer_CurrentStateChanged); 

        fileStream.Close(); 
        fileStream.Dispose(); 

        //Start Recording 
        OnRecordButtonClick(); 
       } 
      } 
      catch (Exception exc) 
      { 
       MessageBox.Show(exc.Message); 
      } 
     } 

     void UpdateRecording(bool isRecording) 
     { 
      if (!isRecording) 
      { 
       using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication()) 
       { 
        spaceTime.Space = storage.AvailableFreeSpace; 
       } 
      } 
      else 
      { 
       spaceTime.Space = memobuffercollection.Count * mphone.GetSampleSizeInBytes(mphone.BufferDuration); 
      } 
      spaceTime.Time = mphone.GetSampleDuration((int)Math.Min(spaceTime.Space, Int32.MaxValue));    
     } 
     void OnMicrophoneBufferReady(object sender, EventArgs e) 
     { 
      // Get buffer from microphone and add to collection 
      byte[] buffer = new byte[mphone.GetSampleSizeInBytes(mphone.BufferDuration)]; 
      int bytesreturned = mphone.GetData(buffer); 
      memobuffercollection.Add(buffer); 

      UpdateRecording(true); 
      // To be Continue... 
      if (spaceTime.Time > TimeSpan.FromMinutes(10)) 
      { 
       StopRecording(); 
       UpdateRecording(false); 
      } 
     } 
     void OnRecordButtonClick() 
     { 
      if (mphone.State == MicrophoneState.Stopped) 
      { 
       // Clear the collection for storing the buffers 
       memobuffercollection.Clear(); 

       // Start Recording 
       mphone.Start(); 
       MyMedia.Play(); 
      } 
      else 
      { 
       MyMedia.Stop(); 
       //mphone.Stop(); 
       PopUpGrid.Visibility = Visibility.Visible; 
       RecordGrid.Opacity = 0.5; 
       RecordGrid.IsHitTestVisible = false; 
      } 
      bool isRecording = mphone.State == MicrophoneState.Started; 
      UpdateRecording(isRecording); 
     } 
     void StopRecording() 
     { 
      // Get the last partial buffer 
      int sampleSize = mphone.GetSampleSizeInBytes(mphone.BufferDuration); 
      byte[] extraBuffer = new byte[sampleSize]; 
      int extraBytes = mphone.GetData(extraBuffer); 

      // Stop Recording 
      mphone.Stop(); 
      //Stop the Song 
      MyMedia.Stop(); 

      // Create MemoInfo object and add at top of collection 
      int totalSize = memobuffercollection.Count * sampleSize + extraBytes; 
      TimeSpan duration = mphone.GetSampleDuration(totalSize); 
      MemoInfo memoInfo = new MemoInfo(DateTime.UtcNow, totalSize, duration); 
      memofiles.Insert(0, memoInfo); 

      // Save Data in IsolatedStorage 
      using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication()) 
      { 
       string[] alldirectories = storage.GetDirectoryNames("NikDirectory"); 
       if (alldirectories.Count() == 0) 
       storage.CreateDirectory("NikDirectory"); 
       try 
       { 
       using (IsolatedStorageFileStream stream = storage.CreateFile("NikDirectory\\" + memoInfo.FileName)) 
       { 
        // Write buffers from collection 
        foreach (byte[] buffer in memobuffercollection) 
         stream.Write(buffer, 0, buffer.Length); 

        // Write partial buffer 
        stream.Write(extraBuffer, 0, extraBytes); 

        stream.Close(); 
        stream.Dispose(); 
       } 

       Uri url = new Uri("/Gallery.xaml", UriKind.Relative); 
       NavigationService.Navigate(url); 
       memobuffercollection.Clear(); 
       } 
       catch (Exception ees) 
       { 
       MessageBox.Show(ees.Message); 
       Uri url = new Uri("/Karaoke.xaml", UriKind.Relative); 
       NavigationService.Navigate(url); 
       } 
      } 
      bool isRecording = mphone.State == MicrophoneState.Started; 
      UpdateRecording(isRecording); 
     } 
} 

所以,请帮我出了问题。我听说在某个地方,当你重定向到另一个屏幕时,你必须处理麦克风的所有物体。这是真的吗?或其他任何东西。

请帮帮我。展望未来。

+0

请分享您尝试过的代码。 –

+0

当然。我将用代码编辑我的帖子。但实际上没有什么更多的变化在该链接 –

回答

0

而不是使用集合,您应该使用以下方法来读取您的记录的字节。 这应该在麦克风对象的BufferReady事件中完成。

byte[] audioBuffer = new byte[microphone.GetSampleSizeInBytes(microphone.BufferDuration)]; 
       microphone.GetData(audioBuffer);     
       RecordingStream.Write(audioBuffer, 0, audioBuffer.Length); 

RecordingStream是MemoryStream,应该在全局声明。

我不确定这个,但正如我已经使用它,它在每种情况下都工作得很好。 试试这个。

+0

感谢您的宝贵意见。但它不会解决我的问题。没有改进。得到相同的录音问题。 –

+0

在我的应用程序中,当我在其他页面上导航时。我清除与记录功能相关的所有对象。我还将录音流设置为空并清除麦克风对象。当我再次回到同一页面时,它开始使用麦克风和流的新对象进行录制。 –

+0

你能告诉我或者把整个代码发给我吗?提前致谢。 –