DirectSound:“值不在预期范围内”.初始化Notify时
本文关键字:范围内 初始化 Notify DirectSound | 更新日期: 2023-09-27 18:09:01
当我尝试运行一个简单的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
属性,但我的猜测是你的问题在哪里。
"如果其BufferDescription.ControlPositionNotify
设置为true
,则SecondaryBuffer对象将仅支持通知事件。"
(https://msdn.microsoft.com/en-us/library/windows/desktop/ms812460.aspx)
那么试试这个:
var description = new BufferDescription
{
BufferBytes = frameSize,
Format = format,
DeferLocation = true,
GlobalFocus = true,
ControlPositionNotify = true
};
我遇到过同样的异常,但这解决了问题!