【发布时间】:2010-09-14 07:44:22
【问题描述】:
有人知道如何使用 C# 以编程方式使 Windows XP 卷静音吗?
【问题讨论】:
-
在Vista/Win7下可以吗?
-
Vista 及以上版本必须使用 IAudioEndpointVolume。
有人知道如何使用 C# 以编程方式使 Windows XP 卷静音吗?
【问题讨论】:
为 P/Invoke 声明这个:
private const int APPCOMMAND_VOLUME_MUTE = 0x80000;
private const int WM_APPCOMMAND = 0x319;
[DllImport("user32.dll")]
public static extern IntPtr SendMessageW(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);
然后使用这条线来静音/取消静音。
SendMessageW(this.Handle, WM_APPCOMMAND, this.Handle, (IntPtr) APPCOMMAND_VOLUME_MUTE);
【讨论】:
public static void ToggleMute(IntPtr handle) { SendMessageW(handle, WM_APPCOMMAND, handle, (IntPtr)APPCOMMAND_VOLUME_MUTE); } 在 WPF 窗口中:VolumeXP.ToggleMute(new WindowInteropHelper(this).Handle);
0x80000? APPCOMMAND_VOLUME_MUTE 定义为8?但是,当我使用 8 时它会失败,但是当我使用你的 0x80000 时它会起作用。这很奇怪。
您可以在 Windows Vista/7 和 8 中使用什么:
您可以使用NAudio。
下载最新版本。提取 DLL 并在 C# 项目中引用 DLL NAudio。
然后添加以下代码以遍历所有可用的音频设备并尽可能将其静音。
try
{
//Instantiate an Enumerator to find audio devices
NAudio.CoreAudioApi.MMDeviceEnumerator MMDE = new NAudio.CoreAudioApi.MMDeviceEnumerator();
//Get all the devices, no matter what condition or status
NAudio.CoreAudioApi.MMDeviceCollection DevCol = MMDE.EnumerateAudioEndPoints(NAudio.CoreAudioApi.DataFlow.All, NAudio.CoreAudioApi.DeviceState.All);
//Loop through all devices
foreach (NAudio.CoreAudioApi.MMDevice dev in DevCol)
{
try
{
//Show us the human understandable name of the device
System.Diagnostics.Debug.Print(dev.FriendlyName);
//Mute it
dev.AudioEndpointVolume.Mute = true;
}
catch (Exception ex)
{
//Do something with exception when an audio endpoint could not be muted
}
}
}
catch (Exception ex)
{
//When something happend that prevent us to iterate through the devices
}
【讨论】:
我遇到了this project,如果您运行的是 Vista,您可能会感兴趣。
【讨论】:
见How to programmatically mute the Windows XP Volume using C#?
void SetPlayerMute(int playerMixerNo, bool value)
{
Mixer mx = new Mixer();
mx.MixerNo = playerMixerNo;
DestinationLine dl = mx.GetDestination(Mixer.Playback);
if (dl != null)
{
foreach (MixerControl ctrl in dl.Controls)
{
if (ctrl is MixerMuteControl)
{
((MixerMuteControl)ctrl).Value = (value) ? 1 : 0;
break;
}
}
}
}
【讨论】:
您可能想要使用 MCI 命令: http://msdn.microsoft.com/en-us/library/ms709461(VS.85).aspx
我应该补充一点,虽然这可以让您在 Windows 中很好地控制输入和输出混音器,但您在进行详细控制时可能会遇到一些困难,例如设置麦克风增强等。
哦,如果您使用的是 Vista,那就别管它了。这是一个完全不同的模型。
【讨论】:
您可以按照此处的说明使用 P/Invoke:http://www.microsoft.com/indonesia/msdn/pinvoke.aspx。它实际上完成了任务 1:靠近顶部的静音和取消静音中的步骤。
【讨论】:
这是 Mike de Klerks 答案的略微改进版本,不需要“on error resume next”代码。
第 1 步:将 NAudio NuGet 包添加到您的项目 (https://www.nuget.org/packages/NAudio/)
第 2 步:使用此代码:
using (var enumerator = new NAudio.CoreAudioApi.MMDeviceEnumerator())
{
foreach (var device in enumerator.EnumerateAudioEndPoints(NAudio.CoreAudioApi.DataFlow.Render, NAudio.CoreAudioApi.DeviceState.Active))
{
if (device.AudioEndpointVolume?.HardwareSupport.HasFlag(NAudio.CoreAudioApi.EEndpointHardwareSupport.Mute) == true)
{
Console.WriteLine(device.FriendlyName);
device.AudioEndpointVolume.Mute = false;
}
}
}
【讨论】: