Làm cách nào để điều chỉnh âm lượng chính trong Windows 7 bằng C#?Tắt/bật tiếng, Thay đổi âm lượng chính trong Windows 7 x64 bằng C#
Tôi đã thấy một thực hiện xuất sắc bằng winmm.dll here, nhưng nó hoạt động với XP và không với Windows 7.
Làm cách nào để điều chỉnh âm lượng chính trong Windows 7 bằng C#?Tắt/bật tiếng, Thay đổi âm lượng chính trong Windows 7 x64 bằng C#
Tôi đã thấy một thực hiện xuất sắc bằng winmm.dll here, nhưng nó hoạt động với XP và không với Windows 7.
CodeProject có một mẫu rất tốt here. Lưu ý rằng nó dựa trên COM interop hoàn toàn (kiểm tra giao diện COM như IAudioEndpointVolume và IAudioMeterInformation trên MSDN nếu bạn quan tâm đến chi tiết triển khai) và chỉ hoạt động cho Vista/Win7 trở lên.
tối thiểu hỗ trợ khách hàng: Windows Vista
tối thiểu hỗ trợ server: Windows Server 2008
tôi sử dụng gói NuGet Naudio thành công với mã này:
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);
}
}
thể trùng lặp http: //stackoverflow.com/a/3046715/285594 – YumYumYum