0

I am working on an Auto Volume Control program that listens to the sound coming from the laptop and sets it to a certain decibel level if it is more than a specified limit.

On researching, I found out about CoreAudioAPIs and even downloaded the Windows 10 SDK to gain access to the APIs.

But I am not able to access the API. I am using C# in Visual Studio Express Edition 2015. When I write "using CoreAudioAPI" , I get the red squiggly under CoreAudioAPI. Same with MMDevice and EndpointVolume APIs, I can't find them!

I tried Adding Reference but the API is not listed.

The code below gives errors:

MMDeviceEnumerator devEnum = new MMDeviceEnumerator();
MMDevice defaultDevice = 
devEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia);

Can someone please tell me how do I access an API in a C# program on a VS Express edition?

Grace A
  • 165
  • 1
  • 12
  • Did you add the assemblies that came with the SDK as references to your project? – rene Sep 05 '17 at 09:06
  • I don't know where to find them or how to add them. Are they the dlls? – Grace A Sep 05 '17 at 09:10
  • 1
    it doesn't look like those API's have a .Net wrapper. Instead look into this: https://stackoverflow.com/questions/31928429/com-objects-c-sharp-casting-mmdeviceenumerator-to-immdeviceenumerator-invalidcas and this: https://stackoverflow.com/questions/14306048/controling-volume-mixer but maybe better check out [NAudio](https://github.com/naudio/NAudio) – rene Sep 05 '17 at 09:15
  • "it doesn't look like those API's have a .Net wrapper" - how did you even know that ? :( I am sorry but I am new at this. I had worked on VB.net a long time ago and never needed to do this. – Grace A Sep 05 '17 at 09:21
  • Because my google search and looking through the MSDN docs don't show anything that points in that direction nor do the seasoned API guru's like Hans Passant mention it. If it existed, he would know and state that as such. – rene Sep 05 '17 at 09:23
  • Can I ask you where you got the information that `using CoreAudioAPI` would be possible? I don't see any evidence that is the case. I'm worried that you are just making this up. – David Heffernan Sep 05 '17 at 10:53
  • https://stackoverflow.com/questions/13139181/how-to-programmatically-set-the-system-volume - this one has it. I saw it in a few other places too. Hope this assures you that I am not making it up. – Grace A Sep 05 '17 at 11:14
  • But I don't see any evidence that you installed that library. – David Heffernan Sep 05 '17 at 16:51
  • I did not. My original question still remains "How to use APIs in Visual Studio?" because I don't know how to "install" it. I hope the person(s) who downvoted my question know of a place on the Internet where this is mentioned. – Grace A Sep 06 '17 at 06:39

1 Answers1

2

The namespace comes from an external library created by xfx and described in their blog post Core Audio for .NET.

The source code of the library is found on GitHub and the zip is linked from the blog post.

Once you either compiled or extracted the binaries you have to add a reference to those dll's from your project.
Make sure you use the browse option and navigate to the folder with the assemblies that contain Core Audio for .NET.

After adding the correct assembly, the namespace will be recognized and your code will compile/no longer complain about the namespace.

rene
  • 41,474
  • 78
  • 114
  • 152
  • MSDN has information on Core Audio APIs but they are different components: https://msdn.microsoft.com/en-us/library/windows/desktop/dd370802(v=vs.85).aspxd. Since there is no mention of C# but has mentions of C++ and header files, maybe the implementation cannot be done as-is in C#? – Grace A Sep 06 '17 at 06:35
  • 2
    @RaniaAsh: The Core Audio APIs are offered as a set of COM interfaces. COM interfaces can easily be accessed in .NET. See [COM Interop](https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/com-interop/) for details. – IInspectable Sep 06 '17 at 10:56
  • Now it's available as a NuGet package as well! – Shayan Jul 11 '22 at 13:33