【问题标题】:Set windows volume from 0 to 100将 Windows 音量设置为 0 到 100
【发布时间】: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。
  • 这能回答你的问题吗? Controlling Application's Volume: By Process-ID
  • 在这里使用 EDataFlow.eCapture 是不明智的。
  • @OlivierRogier 谢谢我将它添加为特定于流程的实现。

标签: c# windows


【解决方案1】:

解决方案 1:主卷

public override void SetMasterVolume(int volume) {
    NativeInterfaces.IMMDeviceEnumerator enumerator = NativeClasses.ComObjectFactory.CreateInstance(new Guid(NativeGuids.MM_DEVICE_ENUMERATOR)) as NativeInterfaces.IMMDeviceEnumerator;

    enumerator.GetDefaultAudioEndpoint(
        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());
}

解决方案 2:工艺比容

public override void SetVolume(List<int> pids, int volume) {
    NativeInterfaces.IMMDeviceEnumerator enumerator = NativeClasses.ComObjectFactory.CreateInstance(new Guid(NativeGuids.MM_DEVICE_ENUMERATOR)) as NativeInterfaces.IMMDeviceEnumerator;

    enumerator.EnumAudioEndpoints(
        NativeEnums.EDataFlow.eRender,
        0x00000001,
        out NativeInterfaces.IMMDeviceCollection deviceCollection
    );

    deviceCollection.GetCount(out uint collectionCount);

    for (uint i = 0; i < collectionCount; i++) {
        deviceCollection.Item(i, out NativeInterfaces.IMMDevice device);
        device.Activate(new Guid(NativeGuids.I_AUDIO_SESSION_MANAGER_2), 0, IntPtr.Zero, out object obj);
        NativeInterfaces.IAudioSessionManager2 audioSessionManager = obj as NativeInterfaces.IAudioSessionManager2;

        audioSessionManager.GetSessionEnumerator(out NativeInterfaces.IAudioSessionEnumerator sessionEnumerator);
        sessionEnumerator.GetCount(out int count);

        for (int j = 0; j < count; j++) {
            sessionEnumerator.GetSession(j, out NativeInterfaces.IAudioSessionControl control);
            NativeInterfaces.IAudioSessionControl2 control2 = control as NativeInterfaces.IAudioSessionControl2;
            control2.GetProcessId(out int pid);

            if (pids.Contains(pid)) {
                NativeInterfaces.ISimpleAudioVolume audioVolume = control as NativeInterfaces.ISimpleAudioVolume;
                audioVolume.SetMasterVolume(volume / 100.0f, Guid.Empty);
            }
        }
    }
}

注意: 解决方案 2 设置相对于当前主音量的音量。因此,如果当前主音量为 50,而您将音频会话的音量设置为 50,则它设置为 25。

【讨论】:

    猜你喜欢
    • 2018-01-14
    • 2010-10-12
    • 2020-10-12
    • 2016-02-24
    • 1970-01-01
    • 1970-01-01
    • 2017-02-03
    • 1970-01-01
    • 2020-02-19
    相关资源
    最近更新 更多