c# - DirectSound:初始化通知时为 "Value does not fall within the expected range."

标签 c# directsound

当我尝试运行一个简单的 DirectSound 程序时,我遇到了那个异常。这是代码:

using System;
using System.Runtime.InteropServices;
using System.Threading;
using Microsoft.DirectX.DirectSound;

namespace AudioDemo
{
    class Program
    {
        [DllImport("user32.dll")]
        private static extern IntPtr GetDesktopWindow();

        static void Main(string[] args)
        {
            // output device

            var device = new Device();
            device.SetCooperativeLevel(GetDesktopWindow(), CooperativeLevel.Normal);

            // format description

            var format = new WaveFormat
            {
                BitsPerSample = 8,
                Channels = 1,
                FormatTag = WaveFormatTag.Pcm,
                SamplesPerSecond = 8000
            };

            format.BlockAlign = (short)(format.BitsPerSample / 8 * format.Channels);
            format.AverageBytesPerSecond = format.SamplesPerSecond * format.BlockAlign;

            var outputLatency = 20;
            var frameSize = format.AverageBytesPerSecond * outputLatency / 1000;

            // buffer 

            var description = new BufferDescription
            {
                BufferBytes = frameSize,
                Format = format,
                DeferLocation = true,
                GlobalFocus = true
            };

            var outputBuffer = new SecondaryBuffer(description, device);

            // buffer notifications

            var notify = new Notify(outputBuffer);

            // ...
        }
    }
}

我在最后一行得到异常 (var notify = new Notify(outputBuffer);)。

不知道哪里出了问题。缓冲区已正确初始化。

最佳答案

我不清楚您要对 outputLatencyframeSize 变量或 BufferBytes 属性做什么,但是我猜你的问题出在哪里。

关于c# - DirectSound:初始化通知时为 "Value does not fall within the expected range.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7444362/

相关文章:

c++ - 如何通过UDP播放音频流?

c# - 在 Linq 中检查两个日期是否相等的优雅解决方案(第二个日期不是参数)

c# - 如何禁用选中列表框中的复选框?

c++ - DirectSound:简单的 pcm 播放

c# - 使用 Directx DirectSound 从麦克风捕获声音

c++ - Visual Studios 忽略文件

winapi - 如何实时处理麦克风输入?

c# - 执行python函数返回结果给c#

c# - 处理过多导致后台 worker 崩溃?

c# - 如何让 while 循环等待一秒钟,将 1 加到一个 int,然后在文本 block 中显示该 int?