Use Windows Volume Mixer to control application volume
]The volume levels of applications can be easily accessed and operated through the Windows core audio library, especially on Windows 7 and later operating systems.
Code example:
]The following C# console application code snippet demonstrates how to control application volume through the volume mixer interface:
using System;
using System.Runtime.InteropServices;
using System.Collections.Generic;
namespace SetAppVolume
{
class Program
{
static void Main(string[] args)
{
const string app = "Mozilla Firefox";
foreach (string name in EnumerateApplications())
{
Console.WriteLine($"应用名称: {name}");
if (name == app)
{
// 显示静音状态和音量级别(相对于主音量百分比)
Console.WriteLine($"静音状态: {GetApplicationMute(app)}");
Console.WriteLine($"音量级别: {GetApplicationVolume(app)}");
// 静音该应用程序
SetApplicationMute(app, true);
// 将音量设置为主音量的一半(50%)
SetApplicationVolume(app, 50);
}
}
}
// 获取应用程序音量
public static float? GetApplicationVolume(string name)
{
ISimpleAudioVolume volume = GetVolumeObject(name);
if (volume == null) return null;
float level;
volume.GetMasterVolume(out level);
return level * 100;
}
// 获取应用程序静音状态
public static bool? GetApplicationMute(string name)
{
ISimpleAudioVolume volume = GetVolumeObject(name);
if (volume == null) return null;
bool mute;
volume.GetMute(out mute);
return mute;
}
// 设置应用程序音量
public static void SetApplicationVolume(string name, float level)
{
ISimpleAudioVolume volume = GetVolumeObject(name);
if (volume == null) return;
Guid guid = Guid.Empty;
volume.SetMasterVolume(level / 100, ref guid);
}
// 设置应用程序静音状态
public static void SetApplicationMute(string name, bool mute)
{
ISimpleAudioVolume volume = GetVolumeObject(name);
if (volume == null) return;
Guid guid = Guid.Empty;
volume.SetMute(mute, ref guid);
}
// 枚举应用程序
public static IEnumerable EnumerateApplications()
{
// 获取扬声器(第一个渲染 多媒体)设备
IMMDeviceEnumerator deviceEnumerator = (IMMDeviceEnumerator)(new MMDeviceEnumerator());
IMMDevice speakers;
deviceEnumerator.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia, out speakers);
// 激活会话管理器。我们需要枚举器
Guid IID_IAudioSessionManager2 = typeof(IAudioSessionManager2).GUID;
object o;
speakers.Activate(ref IID_IAudioSessionManager2, 0, IntPtr.Zero, out o);
IAudioSessionManager2 mgr = (IAudioSessionManager2)o;
// 枚举此设备上的会话
IAudioSessionEnumerator sessionEnumerator;
mgr.GetSessionEnumerator(out sessionEnumerator);
int count;
sessionEnumerator.GetCount(out count);
for (int i = 0; i
Note that this code requires adding a reference to MMDeviceAPI
. The COM interface is used in the code, and the corresponding exceptions need to be handled and ensure that the COM object is properly released to avoid resource leakage. Additionally, Mozilla Firefox
needs to be replaced with the name of the application you want to control. This code is for reference only and may need to be adjusted according to specific circumstances in actual applications.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3