Windows Phone 7 录音问题

3
我将在我的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);
       }
}

所以,请帮我解决这个问题。我听说在重定向到另一个屏幕时,您必须处理麦克风的所有对象,这是真的吗?还是其他什么。

请帮帮我。 期待您的回复。


请分享您尝试过的代码。 - Mahima Gandhe
当然,我会在我的帖子中附上代码。但实际上,在那个链接中没有更多的变化。 - Nikhil Prajapati
1个回答

0

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

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

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

我不确定这一点,但我已经使用过它,在每种情况下都完全正常工作。 试试这个。


感谢您宝贵的反馈。但是它并不能解决我的问题。没有任何改进。仍然存在相同的录音问题。 - Nikhil Prajapati
在我的应用程序中,当我导航到另一个页面时,我清除与录音功能相关联的所有对象。我还将录制流设置为null并清除麦克风对象。当我再次返回相同的页面时,它会使用新的麦克风对象和流开始录音。 - Mahima Gandhe
请问您能否展示或发送给我完整的代码?提前感谢。 - Nikhil Prajapati
请帮帮我。目前我卡在这里了。期待您的帮助。 - Nikhil Prajapati

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接