上一篇介绍了如何枚举声音设备,现在来聊聊如果操作这些声音设备。
目录
-
切换默认声音输出/输入设备
网上搜到的大部分是通过AudioSwitcher来切换,一开始我也用这个,但其搭配的是另一种枚举设备的方式:
AudioDeviceManager.Controller.GetPlaybackDevices(DeviceState state) //获取声音输出设备
AudioDeviceManager.Controller.GetCaptureDevices(DeviceState state) //获取声音输入设备
这种获取声音设备的方式比较恶心,它在某些产品上会出现卡顿的现象,少则卡顿几秒钟,多则卡顿10分钟,这样肯定不行的,所以我就狠心的抛弃了它,转而使用程序集AudioDeviceCmdlets,但是这玩意在NuGet中搜索不到,于是我就直接在网上下载了一个。AudioDeviceCmdlets.dll下载地址https://github.com/frgnca/AudioDeviceCmdlets/releases/download/v3.0/AudioDeviceCmdlets.dll
然后在项目里添加引用既可以使用它了,不管是声音输出设备,还是声音输入设备,都可以用其中的SetDefaultEndpoint方法来实现:
//設定默認音頻設備
PolicyConfigClient client = new PolicyConfigClient();
client.SetDefaultEndpoint(AudioDevice.ID, ERole.eConsole);
client.SetDefaultEndpoint(AudioDevice.ID, ERole.eCommunications);
client.SetDefaultEndpoint(AudioDevice.ID, ERole.eMultimedia);
SetDefaultEndpoint中的参数AudioDevice.ID是上一篇枚举的声音设备的ID,ERole在微软官方定义如下,为了彻底切换,我将eConsole、eMultimedia、eCommunications都进行了设定。
切换默认声音设备之后,还要再判断下是否切换成功,这里使用enumerator.GetDefaultAudioEndpoint来获取默认设备,然后判断下获取的默认设备ID与要设定的设备ID是否一致。enumerator是个什么东西,可以参考上一篇文章的介绍。
//判斷是否設定成功
var defaultDevice = enumerator.GetDefaultAudioEndpoint(dataFlow, NAudio.CoreAudioApi.Role.Console);
if (defaultDevice.ID != AudioDevice.ID)
{
MessageBox.show("切换成功!");
}
else
{
MessageBox.show("切换失败!");
}
-
设定/读取声音设备的音量值
读取音量值,其实在枚举声音设备时,在设备对象属性中就有音量值,包含主音量,左右声道音量。其中MasterVolumeLevelScalar为主音量,Channels[0].VolumeLevelScalar为左声道音量,Channels[1].VolumeLevelScalar为右声道音量。
//根据dataFlow来獲取音頻输入/輸出設備,dataFlow=DataFlow.Render是为输出设备,dataFlow=DataFlow.Capture时为输入设备
var defaultDevice = enumerator.GetDefaultAudioEndpoint(dataFlow, NAudio.CoreAudioApi.Role.Console);
//獲取主音量
int mainVolume = Convert.ToInt16(defaultDevice.AudioEndpointVolume.MasterVolumeLevelScalar * 100);
//當為聲音輸出設備時,获取左右声道音量
if (dataFlow == DataFlow.Render)
{
//左声道音量
int leftVolume = Convert.ToInt16(defaultDevice.AudioEndpointVolume.Channels[0].VolumeLevelScalar * 100);
//右声道音量
int rightVolume = Convert.ToInt16(defaultDevice.AudioEndpointVolume.Channels[1].VolumeLevelScalar * 100);
}
设定音量值,首先获取当前默认设备,然后设定设备对象的MasterVolumeLevelScalar、Channels[0].VolumeLevelScalar、Channels[1].VolumeLevelScalar属性值即可。
//根据dataFlow来獲取音頻输入/輸出設備,dataFlow=DataFlow.Render是为输出设备,dataFlow=DataFlow.Capture时为输入设备
var defaultDevice = enumerator.GetDefaultAudioEndpoint(dataFlow, NAudio.CoreAudioApi.Role.Console);
//設定輸入/輸出設備的主音量大小,targetedMainVolume 为目标主音量值
defaultDevice.AudioEndpointVolume.MasterVolumeLevelScalar = targetedMainVolume / 100.0f;
if (dataFlow == DataFlow.Render)
{
//设定输出设备的左声道音量值,targetedLeftVolume为目标音量值
defaultDevice.AudioEndpointVolume.Channels[0].VolumeLevelScalar = targetedLeftVolume / 100.0f;
//设定输出设备的右声道音量值,targetedRightVolume 为目标音量值
defaultDevice.AudioEndpointVolume.Channels[1].VolumeLevelScalar = targetedRightVolume / 100.0f;
}
-
设定/读取声音设备是否静音
类似于设定音量,也是先获取当前默认设备,然后读取或设定设定设备对象的Mute属性。
//根据dataFlow来獲取音頻输入/輸出設備,dataFlow=DataFlow.Render是为输出设备,dataFlow=DataFlow.Capture时为输入设备
var defaultDevice = enumerator.GetDefaultAudioEndpoint(dataFlow, NAudio.CoreAudioApi.Role.Console);
//獲取設備是否靜音
bool muteVolume = defaultDevice.AudioEndpointVolume.Mute;
//根据dataFlow来獲取音頻输入/輸出設備,dataFlow=DataFlow.Render是为输出设备,dataFlow=DataFlow.Capture时为输入设备
var defaultDevice = enumerator.GetDefaultAudioEndpoint(dataFlow, NAudio.CoreAudioApi.Role.Console);
//設定輸入/輸出設備是否为静音,targetedMute为目标状态,true为静音,false为取消静音
defaultDevice.AudioEndpointVolume.Mute = targetedMute;