当我尝试运行一个简单的 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);
)。
不知道哪里出了问题。缓冲区已正确初始化。
最佳答案
我不清楚您要对 outputLatency
和 frameSize
变量或 BufferBytes
属性做什么,但是我猜你的问题出在哪里。
关于c# - DirectSound:初始化通知时为 "Value does not fall within the expected range.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7444362/