2009-06-23 67 views
1

我試圖使用DirectSound從麥克風捕捉聲音。這裏是我的代碼:C#DirectSound中的模糊呼叫

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

的micBufferDesc和格式變量的實例導致Visual Studio 2008年拋出了以下錯誤:

The call is ambiguous between the following methods or properties: 'Microsoft.DirectX.DirectSound.CaptureBufferDescription.CaptureBufferDescription()' and '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引用,沒有其他東西仍然拋出相同的錯誤。

我還卸載並重新安裝了DirectX SDK(2009年3月)以及DirectX SDK(2008年11月)。仍然沒有運氣。

最後,我在實驗室的另一臺計算機上嘗試了一個新項目,但它仍然無法工作。

下面是引用我有:

  • Microsoft.DirectX.DirectSound
  • Microsoft.DirectX.DirectInput
  • PresentationCore
  • PresentationFramework
  • 服務
  • 系統
  • System.Core程序
  • System.Data
  • System.Data.DataSetExtensions
  • System.Deployment
  • System.Drawing中
  • System.Runtime.Serialization
  • System.ServiceModel
  • 系統。 Windows.Forms
  • System.Xml
  • System.Xml.Linq
  • UIAutomationProvider
  • WindowsBase
  • WindowsFormsIntegration程序
+0

我在帖子中增加了一些信息。這是一個奇怪的問題。 – JohnS 2009-06-24 14:56:13

回答

2

我有同樣的錯誤,它不是一個雙引用。點擊運行,編譯器神奇地忘記它,或者你可以完全停止下面的煩惱。

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 
1

聽起來也許你引用了DirectX組件的多個版本。也許仔細檢查你的參考。如果您需要需要多個版本,那麼extern alias可能會有所幫助 - 但它並不漂亮。


在Visual Studio中,找到「解決方案資源管理」(常見於右側) - 這是在您的項目一切的樹。此樹中的一項是「參考」。這是您的代碼被配置爲使用的外部dll的可視化表示。

(有很多很多的.NET的DLL - 你需要告訴每一個項目是什麼DLL文件可能需要)

展開這個節點,並查找條目2看起來像的DirectX。如果有二,擺脫其中之一(理想情況下與較低版本)。然後嘗試重建。

+0

是的,我檢查了參考文件夾。據我所知,唯一包含這些類的是Microsoft.DirectX.DirectSound,而沒有其他。即使我刪除了其他DirectX引用(Microsoft.DirectX.DirectInput),它仍然不起作用。我應該在我的參考文件夾中發佈所有條目嗎? – JohnS 2009-06-23 19:33:21

+0

嗯....你讓我在那裏... – 2009-06-23 19:52:06

+0

這沒關係。謝謝您的幫助! – JohnS 2009-06-23 19:54:30

0

你已經包含了對包含該函數的程序集的不同版本的兩個引用。刪除其中一個參考。

0

您可能有多個對DirectX程序集的引用。檢查您的項目,在參考文件夾。查找重複的條目,特別是對多個版本的microsoft.directx.directsound.dll的引用。如果有重複,請刪除並重試。

0

這是DirectSound的常見問題。你會發現許多其他問題;)請記住,DS沒有什麼是它看起來。當緩衝區返回null時,它可能只是因爲「讀取位置」只是內部緩衝區寫入指針。因此,當你要求讀或寫指針至少計算一個塊安全區時);並且當你從ds方法獲得緩衝區位置時,使用try cast因爲它可能拋出隨機錯誤。

0

強制你的軟件在x86或x64而不是'任何CPU'編譯將解決這個問題。