【问题标题】:windows phone 7 Recording Issuewindows phone 7 录音问题
【发布时间】:2014-03-06 06:49:34
【问题描述】:

我将在我的 windows phone 7 应用程序中处理录制功能。 我通过这个参考link实现了录制功能。

它在那里完全可以正常工作,在我的情况下也是如此。

实际情况是,在我的应用程序中,我创建了第一页,该页面将用作录制屏幕,与上述链接相同。 当我们停止录音时,我重定向到第二页并将该录音保存在独立存储中,并在第二页绑定了录制的声音。在这里,我播放了录制的声音,效果很好。

现在,当我再次进入录制屏幕(第一页)并开始另一次录制时。有时它会很好地录制,有时它会在录制过程中跳过一些声音,就像哔声一样,而且在录制过程中会出现额外的噪音,并且无法正确录制声音。

我的代码是这样的,

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);
       }
}

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

请帮助我。 期待。

【问题讨论】:

  • 请分享您尝试过的代码。
  • 当然。我将用代码编辑我的帖子。但实际上该引用的链接没有更多变化

标签: windows-phone-7 microphone video-recording


【解决方案1】:

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

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

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

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

【讨论】:

  • 感谢您的宝贵反馈。但这不会解决我的问题。没有任何改进。遇到同样的录音问题。
  • 在我的应用程序中,当我在另一个页面上导航时。我清除了与录制功能相关的所有对象。我还将录制流设置为空并清除麦克风对象。当我再次回到同一页面时,它也开始使用麦克风和流的新对象进行录制。
  • 你能告诉我或把整个代码发给我吗?提前致谢。
  • 请帮助我。然而现在,我被困在这里。期待。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-20
  • 1970-01-01
  • 1970-01-01
  • 2011-09-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多