C# Audio全自动化测试——2. Audio设备操作

上一篇介绍了如何枚举声音设备,现在来聊聊如果操作这些声音设备。

C# Audio全自动化测试——1. 枚举Audio设备_只会搬运的小菜鸟的博客-CSDN博客

目录

切换默认声音输出/输入设备 

设定/读取声音设备的音量值

设定/读取声音设备是否静音


  • 切换默认声音输出/输入设备 

网上搜到的大部分是通过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;

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

只会搬运的小菜鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值