2014-03-06 141 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

你能告訴我或者把整個代碼發給我嗎?提前致謝。 –