【发布时间】:2021-01-12 00:22:01
【问题描述】:
我想直接将windows中音频端点的音量设置为0到100
public override void SetVolume(int volume) {
NativeInterfaces.IMMDeviceEnumerator enumerator = NativeClasses.ComObjectFactory.CreateInstance(new Guid(NativeGuids.MM_DEVICE_ENUMERATOR)) as NativeInterfaces.IMMDeviceEnumerator;
enumerator.GetDefaultAudioEndpoint(
NativeEnums.EDataFlow.eCapture | NativeEnums.EDataFlow.eRender,
NativeEnums.ERole.eConsole,
out NativeInterfaces.IMMDevice device
);
device.Activate(new Guid(NativeGuids.I_AUDIO_ENDPOINT_VOLUME), 0x1, IntPtr.Zero, out object volumeObj);
NativeInterfaces.IAudioEndpointVolume audioEndpointVolume = volumeObj as NativeInterfaces.IAudioEndpointVolume;
audioEndpointVolume.SetMasterVolumeLevelScalar(volume / 100.0f, new Guid());
}
当我运行此代码时,尽管我将 60 作为参数传递,但我的音量仍为 50。当我调用 GetMasterVolumeLevelScalar 时,它输出 0.6 所以代码有效,但似乎我使用了错误的方法。那么如何设置0到100之间呢?
【问题讨论】:
-
请在运行程序后发布the Windows Volume Mixer window 的屏幕截图。我认为您只是在更改进程的音量级别,而不是系统范围的音量级别。
-
已添加。混音器中有第五个,OBS 也是 50。
-
在这里使用 EDataFlow.eCapture 是不明智的。
-
@OlivierRogier 谢谢我将它添加为特定于流程的实现。