简体   繁体   English

DirectSound中的C#模糊调用

[英]C# Ambiguous call in DirectSound

I'm trying to use DirectSound to capture sound from a microphone. 我正在尝试使用DirectSound从麦克风捕获声音。 Here's my code: 这是我的代码:

    using Microsoft.DirectX.DirectSound;
    public MicrophoneSensor()
    {
            CaptureBufferDescription micBufferDesc = new CaptureBufferDescription();
            WaveFormat format = new WaveFormat();
            format.SamplesPerSecond = 22000;
            format.Channels = 1;
            format.BitsPerSample = 8;
            format.AverageBytesPerSecond = 22000;
            format.BlockAlign = 1;

            micBufferDesc.Format = format;
            micBufferDesc.BufferBytes = 100000;
            micBufferDesc.ControlEffects = false;
            micBufferDesc.WaveMapped = true;

            micBuffer = new CaptureBuffer(micBufferDesc, microphone);
     }

The instantiations of the micBufferDesc and format variables cause Visual Studio 2008 to throw the following error: micBufferDesc和format变量的实例化导致Visual Studio 2008引发以下错误:

The call is ambiguous between the following methods or properties: 'Microsoft.DirectX.DirectSound.CaptureBufferDescription.CaptureBufferDescription()' and 'Microsoft.DirectX.DirectSound.CaptureBufferDescription.CaptureBufferDescription()' 该调用在以下方法或属性之间是不明确的:'​​Microsoft.DirectX.DirectSound.CaptureBufferDescription.CaptureBufferDescription()'和'Microsoft.DirectX.DirectSound.CaptureBufferDescription.CaptureBufferDescription()'

and

The call is ambiguous between the following methods or properties: 'Microsoft.DirectX.DirectSound.WaveFormat.WaveFormat()' and 'Microsoft.DirectX.DirectSound.WaveFormet.WaveFormat()' 下列方法或属性之间的调用不明确:'Microsoft.DirectX.DirectSound.WaveFormat.WaveFormat()'和'Microsoft.DirectX.DirectSound.WaveFormet.WaveFormat()'

I've tried quite a few different combinations of stating the namespace and using statements but no luck. 我已经尝试了许多种不同的组合来说明名称空间和使用语句,但是没有运气。

I've also checked the references in the solution explorer and as far as I can tell there are no duplicates. 我还检查了解决方案资源管理器中的引用,据我所知没有重复项。

A brand new test project with only the Microsoft.DirectX.DirectSound reference and nothing else still throws the same error. 一个仅具有Microsoft.DirectX.DirectSound引用且没有其他任何东西的全新测试项目仍会引发相同的错误。

I have also uninstalled and reinstalled the DirectX SDK (March 2009) as well as the DirectX SDK (November 2008). 我还卸载并重新安装了DirectX SDK(2009年3月)和DirectX SDK(2008年11月)。 Still no luck. 仍然没有运气。

Finally, I've tried a new project on another computer here in the lab and it still doesn't work. 最后,我在实验室的另一台计算机上尝试了一个新项目,但仍然无法正常工作。

Here are the references I have: 这是我的参考资料:

  • Graph 图形
  • Microsoft.DirectX.DirectSound Microsoft.DirectX.DirectSound
  • Microsoft.DirectX.DirectInput Microsoft.DirectX.DirectInput
  • PresentationCore PresentationCore
  • PresentationFramework PresentationFramework
  • Service 服务
  • System 系统
  • System.Core System.Core程序
  • System.Data System.Data
  • System.Data.DataSetExtensions System.Data.DataSetExtensions
  • System.Deployment System.Deployment
  • System.Drawing System.Drawing中
  • System.Runtime.Serialization System.Runtime.Serialization
  • System.ServiceModel System.ServiceModel
  • System.Windows.Forms System.Windows.Forms的
  • System.Xml 的System.Xml
  • System.Xml.Linq System.Xml.Linq的
  • UIAutomationProvider UIAutomationProvider
  • WindowsBase WindowsBase
  • WindowsFormsIntegration WindowsFormsIntegration程序

I had the same error, it's not a double reference. 我有同样的错误,不是双重参考。 Click run and the compiler magically forgets it, or you can stop the annoyance completely with the following. 单击运行,编译器会神奇地忘记它,或者您可以通过以下操作完全消除烦恼。

using System.Reflection;

// then instead of WaveFormat fmt = new WaveFormat()

ConstructorInfo constructor = typeof(WaveFormat).GetConstructor(Type.EmptyTypes);
WaveFormat fmt = (WaveFormat)constructor.Invoke(null);

// do the same for CaptureBufferDescription

It sounds like maybe you are referencing multiple versions of the directx assembly. 听起来您可能正在引用DirectX程序集的多个版本。 Perhaps double-check your references. 也许仔细检查您的引用。 If you need multiple versions, then extern alias may help - but it isn't pretty. 如果您需要多个版本,则extern alias可能会有所帮助-但这不是很漂亮。


In Visual Studio, look for the "solution explorer" (commonly on the right hand side) - this is the tree of everything in your project. 在Visual Studio中,查找“解决方案资源管理器”(通常在右侧)-这是项目中所有内容的树。 One of the items in this tree is the "References". 该树中的一项是“参考”。 This is a visual representation of the external dlls that your code is configured to make use of. 这是将代码配置为使用的外部dll的直观表示。

(there are many, many .NET dlls - you need to tell each project what dlls it might need) (.NET DLL有很多很多,您需要告诉每个项目它可能需要哪些DLL)

Expand this node, and look for 2 entries that look like directx. 展开此节点,然后查找2个看起来像DirectX的条目。 If there are two, get rid of one of them (ideally the one with the lower version). 如果两个,摆脱他们的一个(理想情况下具有较低版本)的。 Then try and rebuild. 然后尝试重新构建。

you've included two references to different versions of the assembly that contains that function. 您已经包含了对包含该函数的程序集的不同版本的两个引用。 Remove one of the references. 删除引用之一。

You may have multiple references to the DirectX assembly. 您可能对DirectX程序集有多个引用。 Check your project, in the References folder. 在“引用”文件夹中检查您的项目。 Look for duplicate entries, specifically references to multiple version of microsoft.directx.directsound.dll. 查找重复的条目,特别是对microsoft.directx.directsound.dll的多个版本的引用。 If there are duplicates remove one and try again. 如果有重复,请删除一个并重试。

强制您的软件在x86或x64而非“任何CPU”中进行编译将解决此问题。

This is common problem with DirectSound. 这是DirectSound的常见问题。 You will find also many many other problems ;) Remember that with DS nothing is what it looks. 您还会发现许多其他问题;)请记住,使用DS看起来并不像什么。 When buffer return null it probably cause only because "read position" is just inner buffer write pointer. 当缓冲区返回null时,可能仅是因为“读取位置”仅是内部缓冲区写指针。 So when you ask read or write pointers calculate allways at least one block safe zone ;) and when you get buffer positions from ds methods use try cast because it can throw random errors. 因此,当您要求读写指针始终计算至少一个块安全区时;),并且当您从ds方法获取缓冲区位置时,请使用try cast,因为它会引发随机错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM