【问题标题】:Help with capture sound from microphone and play it back in C#?帮助从麦克风捕获声音并在 C# 中播放?
【发布时间】:2010-03-03 18:41:26
【问题描述】:

您好,我收到了这段代码,但我遇到了 2 个无法摆脱的错误。

有什么帮助吗?

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

谢谢!

【问题讨论】:

  • 知道您的 2 个错误发生在哪里会有所帮助。你能提供一个堆栈跟踪吗?
  • 你能给我你的个人邮箱,我可以把程序发给你吗?
  • 通过评论 //got error here 标记了错误发生的位置
  • 你遇到了什么错误?运行时错误?堆栈跟踪是什么?编译错误?编译器错误消息的文本是什么?
  • 错误是:'System.array' 不包含'Offset' 的定义并且没有扩展方法'Offset' 接受'System.Array' 类型的第一个参数,并且'System.Array' 可以找到。 array' 不包含 'EventNotifyHandle' 的定义,并且找不到接受第一个参数类型为 'System.Array' 的扩展方法 'EventNotifyHandle'

标签: c# directx


【解决方案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();
        }

【讨论】:

  • 您的回答消除了错误,但该程序似乎无法运行:S 他们有没有机会将我的整个程序发送给您?谢谢
  • 所以我现在代码正在运行,我得到了一个我无法摆脱的已处理异常。有什么帮助吗?谢谢
【解决方案2】:

我尝试了代码。首先它没有工作。 而不是:private BufferPositionNotify[] _positionNotify = new BufferPositionNotify[_bufferPositions + 1]; 应该是 private BufferPositionNotify[] _positionNotify = new BufferPositionNotify[_bufferPositions];。否则,您的数组中有太多元素。

【讨论】:

    猜你喜欢
    • 2011-10-06
    • 2016-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-18
    • 2013-06-28
    • 1970-01-01
    相关资源
    最近更新 更多