2

当我尝试运行一个简单的 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);我在最后一行( )上得到了异常。

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

4

2 回答 2

0

我不清楚你想用你的outputLatency变量frameSizeBufferBytes属性做什么,但我猜你的问题出在哪里。

于 2011-09-16T13:00:06.773 回答
0

只有将 SecondaryBuffer 对象设置为时,它才会支持通知事件。BufferDescription.ControlPositionNotifytrue

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

我有同样的例外,但这解决了问题!

于 2016-05-22T05:38:56.773 回答