2010-03-03 81 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];。另外,你有一个元素在你的数组中。

相关问题