【问题标题】:Switching current active sound device using VBScript?使用 VBScript 切换当前的活动声音设备?
【发布时间】:2014-02-15 16:39:38
【问题描述】:
我想在连接到我的计算机的两个音频设备之间切换(Windows 7 32 位)。我看了question,发现nircmd。
现在,我可以创建两个 VBS 文件来在两个设备之间切换。我想知道是否可以找出当前的活动/默认声音设备是什么,然后我可以将所有内容放在一个文件中。
我当前的代码是 -
Set WshShell = CreateObject("WScript.Shell")
cmds=WshShell.RUN("L:\MyApps\NirCmd\nircmd.exe setdefaultsounddevice ""Speakers""", 0, True)
Set WshShell = Nothing
另一个文件有“耳机”而不是“扬声器”。
【问题讨论】:
标签:
audio
windows-7
vbscript
【解决方案1】:
这就是我目前解决的方法。这很糟糕,我知道。它没有找到当前活动的设备,而是将信息存储在一个文本文件中(必须已经存在!其中包含当前设备名称!)。是的,这太可怕了。但考虑到我对 VBScript 的了解和目前对 Windows 注册表的了解,这两者都非常接近于零,这是我能想到的最好的了!
我在这里发布这个,因为在其他任何地方都没有简单的答案。如果有人在不使用任何其他程序的情况下有任何更好的解决方案,我将非常感激。
另外,如果有人想使用此代码,请更改文件路径和名称以适合您的。
Dim objFso, objFileHandle, strDisplayString
Set objFso = WScript.CreateObject("Scripting.FileSystemObject")
Set readObjFileHandle = objFso.OpenTextFile("L:\MyApps\NirCmd\CurrentDevice.txt", 1)
strDisplayString = readObjFileHandle.ReadLine()
readObjFileHandle.Close
Set writeObjFileHandle = objFso.OpenTextFile("L:\MyApps\NirCmd\CurrentDevice.txt", 2, "True")
If StrComp(strDisplayString, "Headphones", vbTextCompare) = 0 Then
'MsgBox "Headphones - switching to Speakers"
Set WshShell = CreateObject("WScript.Shell")
cmds=WshShell.RUN("L:\MyApps\NirCmd\nircmd.exe setdefaultsounddevice ""Speakers""", 2, True)
Set WshShell = Nothing
writeObjFileHandle.Write("Speakers")
Else
'MsgBox "Speakers - switching to Headphones"
Set WshShell = CreateObject("WScript.Shell")
cmds=WshShell.RUN("L:\MyApps\NirCmd\nircmd.exe setdefaultsounddevice ""Headphones""", 2, True)
Set WshShell = Nothing
writeObjFileHandle.Write("Headphones")
End If
writeObjFileHandle.Close