[英]How to identify the default audio device in Powershell?
I am looking for a solution to get the default audio device via Powershell.我正在寻找通过 Powershell 获取默认音频设备的解决方案。 In best case, it could work via embedded C#-code to directly use IMMDeviceEnumerator::GetDefaultAudioEndpoint (see here IMMDeviceEnumertor ).
在最好的情况下,它可以通过嵌入式 C# 代码直接使用 IMMDeviceEnumerator::GetDefaultAudioEndpoint(请参阅此处IMMDeviceEnumertor )。
But if it is easier to get this via RegKeys, then this is also OK.但是如果通过 RegKeys 更容易获得它,那么这也可以。 I have seen a couple of code-snippets reading the keys from HKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\MMDevices\\Audio\\Render or \\Capture, but I still struggle to identify the DEFAULT device.
我已经看到一些代码片段从 HKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\MMDevices\\Audio\\Render 或 \\Capture 读取密钥,但我仍然很难识别默认设备。
It seems, when I do modify the order of devices, then I can simply search for active devices (DeviceState=1) and then sort by the values "Level:0", "Level:1" and "Level:2", but the level-values are not available on a system, where the user has not modied the order manually.看来,当我修改设备的顺序时,我可以简单地搜索活动设备(DeviceState=1),然后按值“Level:0”、“Level:1”和“Level:2”排序,但是级别值在用户未手动修改订单的系统上不可用。 What is the sort-criteria in such case?
在这种情况下,排序标准是什么?
This is the code-snippet to solve it via RegKeys, but as mentioned - not working for all situations:这是通过 RegKeys 解决它的代码片段,但如前所述 - 不适用于所有情况:
$regAudio = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\MMDevices\Audio"
$nameId = "{b3f8fa53-0004-438e-9003-51a46e139bfc},6"
$classId = "{a45c254e-df1c-4efd-8020-67d146a850e0},2"
$driverDetails = "{83da6326-97a6-4088-9453-a1923f573b29},3"
function get-DefaultDevice($type) {
$activeDevices = foreach($key in Get-ChildItem "$regAudio\$type\") {
foreach($item in Get-ItemProperty $key.PsPath) {
if ($item.DeviceState -eq $activeState) {$item}
}
}
$defaultDevice = $activeDevices | Sort-Object -Property "Level:0","Level:1","Level:2" | select -last 1
$details = Get-ItemProperty "$($defaultDevice.PSPath)\Properties"
$name = "$($details.$classId) ($($details.$nameId))"
return @{
name = $name
driver = $details.$driverDetails
}
}
$OsRender = get-DefaultDevice "Render"
$OsCapture = get-DefaultDevice "Capture"
Is there any way to get this info "in a smart way" (without any external DLLs, of course)?有什么方法可以“以智能方式”获取此信息(当然,没有任何外部 DLL)?
Finally I figured it out and I am happy to share the working code-snippet:最后我想通了,我很高兴分享工作代码片段:
cls
Add-Type -TypeDefinition @'
using System.Runtime.InteropServices;
[Guid("D666063F-1587-4E43-81F1-B948E807363F"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IMMDevice {
int a(); int o();
int GetId([MarshalAs(UnmanagedType.LPWStr)] out string id);
}
[Guid("A95664D2-9614-4F35-A746-DE8DB63617E6"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IMMDeviceEnumerator {
int f();
int GetDefaultAudioEndpoint(int dataFlow, int role, out IMMDevice endpoint);
}
[ComImport, Guid("BCDE0395-E52F-467C-8E3D-C4579291692E")] class MMDeviceEnumeratorComObject { }
public class Audio {
public static string GetDefault (int direction) {
var enumerator = new MMDeviceEnumeratorComObject() as IMMDeviceEnumerator;
IMMDevice dev = null;
Marshal.ThrowExceptionForHR(enumerator.GetDefaultAudioEndpoint(direction, 1, out dev));
string id = null;
Marshal.ThrowExceptionForHR(dev.GetId(out id));
return id;
}
}
'@
function getFriendlyName($id) {
$reg = "HKLM:\SYSTEM\CurrentControlSet\Enum\SWD\MMDEVAPI\$id"
return (get-ItemProperty $reg).FriendlyName
}
$id0 = [audio]::GetDefault(0)
$id1 = [audio]::GetDefault(1)
write-host "Default Speaker: $(getFriendlyName $id0)"
write-host "Default Micro : $(getFriendlyName $id1)"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.