Csharp/C#教程:静音/取消静音,使用C#在Windows 7 x64中更改主音量分享


静音/取消静音,使用C#在Windows 7 x64中更改主音量

如何使用C#在Windows 7中调整主音量?

我在这里看到了使用winmm.dll的优秀实现,但它适用于XP而不适用于Windows 7。

CodeProject 在这里有一个非常好的示例。 请注意,它完全依赖于COM互操作(如果您对实现细节感兴趣,请在MSDN上检查COM接口,如IAudioEndpointVolume和IAudioMeterInformation ),并且仅适用于Vista / Win7及更高版本。

支持的最低客户端:Windows Vista

支持的最低服务器:Windows Server 2008

我用这段代码成功使用了nuget包Naudio:

上述就是C#学习教程:静音/取消静音,使用C#在Windows 7 x64中更改主音量分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

public void SetVolume(int level) { 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 { if (dev.State == NAudio.CoreAudioApi.DeviceState.Active) { var newVolume = (float)Math.Max(Math.Min(level, 100),0) / (float)100; //Set at maximum volume dev.AudioEndpointVolume.MasterVolumeLevelScalar = newVolume; dev.AudioEndpointVolume.Mute = level == 0; //Get its audio volume _log.Info("Volume of " + dev.FriendlyName + " is " + dev.AudioEndpointVolume.MasterVolumeLevelScalar.ToString()); } else { _log.Debug("Ignoring device " + dev.FriendlyName + " with state " + dev.State); } } catch (Exception ex) { //Do something with exception when an audio endpoint could not be muted _log.Warn(dev.FriendlyName + " could not be muted with error " + ex); } } } catch (Exception ex) { //When something happend that prevent us to iterate through the devices _log.Warn("Could not enumerate devices due to an excepion: " + ex.Message); } } 

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/953682.html

(0)
上一篇 2021年11月19日
下一篇 2021年11月19日

精彩推荐