windows 如何以编程方式获取当前音频电平?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3992798/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to programmatically get the current audio level?
提问by David F.
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 audio device or not.
基本上,我需要的是一种利用当前音频输出并检查声级的方法,即我需要能够检查音频设备上是否正在播放某些内容。
I do not need to check the volume setting, but the actual playing audio stream's sound level.
我不需要检查音量设置,而是检查实际播放音频流的声级。
Sorry, I was asking about how to do it in Windows, on Visual Studio 2008.
抱歉,我问的是如何在 Visual Studio 2008 上的 Windows 中执行此操作。
@mikerobi: That forms a part of my reasoning - if it is being displayed on the system volume meter, there must be a system call that can get it back
@mikerobi:这构成了我推理的一部分 - 如果它显示在系统音量计上,则必须有一个系统调用可以将其取回
回答by KeithS
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:
这是一个很好的问题。对于 32 位 Windows 应用程序,答案是挂钩 winmm.dll 和其他低级音频控制 DLL。在 C# 中,我将创建一个包含外部方法原型的包装类:
public class MyAudioWrapper
{
[DllImport("winmm.dll", EntryPoint = "waveOutGetVolume")]
public extern void GetWaveVolume(IntPtr devicehandle, out int Volume);
...
}
Have a look at this linkfor 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.
查看此链接以获取 Windows 音频方法列表;您可以使用混音器或仅使用波形输出控制器来设置音量。您要使用的内容将决定要导入的库。您必须研究如何最好地定义原型,以及如何获得音频/混音器设备的句柄。
回答by A_Nabelsi
Here is a helpful link for Windows API invokations, and here's exactly what you are looking for:
这是 Windows API 调用的有用链接,这正是您要查找的内容:
http://www.pinvoke.net/default.aspx/winmm.waveOutGetVolume
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
我认为您需要阅读输出流上正在播放的内容,并通过分析某些算法中的数据,您可能能够确定是否正在播放某些内容。为此,您需要MMDevice API
http://msdn.microsoft.com/en-us/library/dd316556(v=VS.85).aspx
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.
即使你做到了,你也无法预测设备的结果行为。祝你好运。
回答by Vantomex
I've recently answered such a question here, see How to detect if any sound plays on a Windows machine.
我最近在这里回答了这样一个问题,请参阅如何检测 Windows 机器上是否有声音播放。