2

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?

XSL
  • 2,965
  • 7
  • 38
  • 61

3 Answers3

4

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;
}
FIL
  • 1,148
  • 3
  • 15
  • 27
2

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.

Community
  • 1
  • 1
John Riselvato
  • 12,854
  • 5
  • 62
  • 89
0

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.

Brad
  • 159,648
  • 54
  • 349
  • 530
  • 1
    The example John Reselvato posted works on my machine and I guess that it controls the sound card mixer. That's what I'd like to achieve (although the sample posted is very bloated and somewhat complicated to dissect just the volume part). – XSL Mar 04 '12 at 13:35
  • @SSL, Oh, I thought you wanted to control the PC Speaker. Yes, the winmm API has quite a bit to it. – Brad Mar 04 '12 at 16:39