2010-03-03 78 views
1

嗨,我得到了這段代碼,我得到了2個錯誤,我無法擺脫。幫助捕捉來自麥克風的聲音並在C#中播放它?

任何幫助嗎?

namespace pleasework 
{ 
    public partial class Form1 : Form 
    { 
     private Thread _echoThread; 
     private Capture _captureDevice; 
     private CaptureBuffer _captureBuffer; 
     private Device _playbackDevice; 
     private SecondaryBuffer _playbackBuffer; 
     private int _bufferSize; 
     private const int _bufferPositions = 4; 
     private AutoResetEvent _notificationEvent = null; 
     private BufferPositionNotify[] _positionNotify = new BufferPositionNotify[_bufferPositions + 1]; 
     private Notify _echoNotify = null; 

     private void EchoThread() 
     { 
      int offset = 0; 



      _captureBuffer.Start(true); 
      _playbackBuffer.Play(0, BufferPlayFlags.Looping); 
      //byte[] buffer = (byte[])_captureBuffer.Read(offset, typeof(byte), LockFlag.FromWriteCursor, _bufferSize); 


      for (; ;) 
      { 
       _notificationEvent.WaitOne(Timeout.Infinite, true); 

       byte[] buffer = (byte[])_captureBuffer.Read(offset, typeof(byte), LockFlag.None, _bufferSize); 

       _playbackBuffer.Write(offset, buffer, LockFlag.None); 
       offset = (offset + _bufferSize) % (_bufferPositions * _bufferSize); 
      } 

     } 

     public Form1() 
     { 
      CheckForIllegalCrossThreadCalls = false; 
      InitializeComponent(); 

      _captureDevice = new Capture(); 

      short channels = 2; 


      short bitsPerSample = 16; 


      int samplesPerSecond = 22050; 

      //Set up the wave format to be captured 
      WaveFormat waveFormat = new WaveFormat(); 
      waveFormat.Channels = channels; 
      waveFormat.FormatTag = WaveFormatTag.Pcm; 
      waveFormat.SamplesPerSecond = samplesPerSecond; 
      waveFormat.BitsPerSample = bitsPerSample; 
      waveFormat.BlockAlign = (short)(channels * (bitsPerSample/8)); 
      waveFormat.AverageBytesPerSecond = waveFormat.BlockAlign * samplesPerSecond; 

      _bufferSize = waveFormat.AverageBytesPerSecond/20; 

      CaptureBufferDescription captureBufferDescription = new CaptureBufferDescription(); 
      captureBufferDescription.BufferBytes = _bufferPositions * _bufferSize; 
      captureBufferDescription.Format = waveFormat; 
      _captureBuffer = new CaptureBuffer(captureBufferDescription, _captureDevice); 

      _playbackDevice = new Device(); 
      _playbackDevice.SetCooperativeLevel(this, CooperativeLevel.Normal); 

      BufferDescription playbackBufferDescription = new BufferDescription(); 
      playbackBufferDescription.BufferBytes = _bufferPositions * _bufferSize; 
      playbackBufferDescription.Format = waveFormat; 
      _playbackBuffer = new SecondaryBuffer(playbackBufferDescription, _playbackDevice); 

      _echoThread = new Thread(new ThreadStart(EchoThread)); 
      _echoThread.Start(); 


      _notificationEvent = new AutoResetEvent(false); 
      for (int i = 0; i < _bufferPositions; i++) 
      { 

       _positionNotify.Offset = (_bufferSize * i) + _bufferSize - 1; // got error here 
       _positionNotify.EventNotifyHandle = _notificationEvent.SafeWaitHandle.DangerousGetHandle();// got error here 
      } 
      _echoNotify = new Notify(_captureBuffer); 
      _echoNotify.SetNotificationPositions(_positionNotify, _bufferPositions); 

     } 

     private void EchoClose(object sender, FormClosingEventArgs e) 
     { 
      _echoThread.Abort(); 
     } 
    } 
} 

謝謝!

+0

它有助於知道你的2錯誤發生在哪裏。你能提供一個堆棧跟蹤嗎? – Aaron 2010-03-03 18:45:45

+0

你可以給我你的個人電子郵件,所以我可以給你的程序? – 2010-03-03 18:47:28

+0

ive標記錯誤發生在哪裏通過評論//在這裏得到錯誤 – 2010-03-03 18:57:28

回答

1

您正在將_positionNotify設置爲_bufferPosition + 1元素的數組。然而,當你在for循環中時,你永遠不會指定爲Offset和EventNotifyHandle設置哪些元素。此外,我認爲你需要添加一條額外的線,所以實際上創建一個BufferPositionNotify結構的新實例。嘗試改變這些行像這樣:

 for (int i = 0; i < _bufferPositions; i++) 
     { 
      _positionNotify[i] = new BufferPositionNotify(); 
      _positionNotify[i].Offset = (_bufferSize * i) + _bufferSize - 1; 
      _positionNotify[i].EventNotifyHandle = 
       _notificationEvent.SafeWaitHandle.DangerousGetHandle(); 
     } 
+0

你的答案消除了錯誤,但它看起來程序沒有運行:S是他們的任何機會我可以發送給我你的整個程序嗎? 謝謝 – 2010-03-03 20:49:49

+0

它工作感謝很多隊友!你救我的屁股 – 2010-03-03 20:58:32

+0

所以我現在的代碼正在工作,現在我得到了一個處理的異常,我無法擺脫。任何幫助? 謝謝 – 2010-03-06 14:25:21

0

我試過了代碼。首先它不起作用。 而不是:private BufferPositionNotify[] _positionNotify = new BufferPositionNotify[_bufferPositions + 1];它應該是private BufferPositionNotify[] _positionNotify = new BufferPositionNotify[_bufferPositions];。另外,你有一個元素在你的數組中。

相關問題