[英]Mute Windows Volume using C#
有人知道如何使用 C# 以編程方式使 Windows XP Volume 靜音嗎?
為 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);
你可以在 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
}
請參閱如何使用 C# 以編程方式將 Windows XP 卷靜音?
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 答案的略微改進版本,不需要“下一個錯誤恢復”代碼。
步驟 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;
}
}
}
CoreAudioDevice defaultPlaybackDevice = new
CoreAudioController().DefaultPlaybackDevice;
if (!defaultPlaybackDevice.IsMuted)
defaultPlaybackDevice.ToggleMute();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.