Mute system from code

跟風遠走 提交于 2019-12-08 08:16:12

问题


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;
}


来源:https://stackoverflow.com/questions/9552208/mute-system-from-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!