How to programmatically get the current audio level?

前端 未结 3 1652
梦谈多话
梦谈多话 2020-12-31 09:31

Basically, what I need is a way to tap into the current audio output and check the sound level, i.e. I need to be able to check whether there is something playing on the aud

相关标签:
3条回答
  • 2020-12-31 10:04

    This is a good question. The answer, for 32-bit Windows apps, is to hook into winmm.dll and other low-level audio control DLLs. In C# I'd create a wrapper class containing extern method prototypes:

    public class MyAudioWrapper
    {
       [DllImport("winmm.dll", EntryPoint = "waveOutGetVolume")]
       public extern void GetWaveVolume(IntPtr devicehandle, out int Volume);
    
       ...
    }
    

    Have a look at this link for a list of Windows audio methods; you can use the mixer, or just the wave-out controller, to set volume. What you want to use will dictate what libraries to import. You'll have to research how best to define the prototype, and how to get the handle to the audio/mixer device.

    0 讨论(0)
  • 2020-12-31 10:07

    Here is a helpful link for Windows API invokations, and here's exactly what you are looking for:

    http://www.pinvoke.net/default.aspx/winmm.waveOutGetVolume


    Since the requirement changed and you don't need the audio level I suggest the following might help:

    I think you need to read what is being playedback on the output stream and by analyzing the data in some algorithms you might be able to decide weather something is being playedback or not. To do this you need the MMDevice API

    http://msdn.microsoft.com/en-us/library/dd316556(v=VS.85).aspx

    I don't want to discorage you but believe me this is not going to be easy to accomplish if you are not familiar with unmanaged code.

    • You have to fill many structures in each invokation.
    • You have to perform invokations in specific order.
    • Marshalling references to structures.

    And even if you accomplish that you can't anticipate the outcome behavior of the device. Good luck.

    0 讨论(0)
  • 2020-12-31 10:17

    I've recently answered such a question here, see How to detect if any sound plays on a Windows machine.

    0 讨论(0)
提交回复
热议问题