Mute system from code

2019-06-02 03:53发布

问题:

I'm trying to mute the PC speaker from my C# console application. I've tried the code suggested on this site and it doesn't affect the volume on my machine. I need the code to work on Win7 and I assume that code only works on XP. I also tried this:

[DllImport("winmm.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi)]
public static extern int waveOutSetVolume(IntPtr uDeviceID, int dwVolume);

waveOutSetVolume(IntPtr.Zero, 0);

But when the waveOutSetVolume method is called, the system is not muted and no error are thrown. Is there a way to mute the PC from C# in Win7?

回答1:

I believe this is what you are looking for. this has been test with windows 7 and also this question as been asked on SO before. Which probably will help you get what you are looking for.



回答2:

You cannot mute the PC speaker. It is a basic device, has no mixer, etc. What comes out of it is a constant volume.

What you show in your example is code to control your sound card's mixer, which is a completely different device.



回答3:

You can use AudioSwitcher.AudioApi.CoreAudio extension for the AudioSwitcher library. Just install it as a Nuget package using Package Manager Console:

Install-Package AudioSwitcher.AudioApi.CoreAudio

It has a methods like Mute, ToggleMute and the Volume property, so you can set the value you want.

Here is an example:

using (var audio = new CoreAudioController())
{
    audio.DefaultPlaybackDevice.Volume = 50;
}