0

I want to switch between two audio devices connected to my computer (Windows 7 32 bit). I had a look at question, and found nircmd.

Now, I'm able to create two VBS files to switch between the two devices. I was wondering if I could find out what the current active/default sound device is, then I could put everything in one file itself.

My current code is -

Set WshShell = CreateObject("WScript.Shell")
cmds=WshShell.RUN("L:\MyApps\NirCmd\nircmd.exe setdefaultsounddevice ""Speakers""", 0, True)
Set WshShell = Nothing

The other file has "Headphones" instead of "Speakers".

Community
  • 1
  • 1
abhijit
  • 6,363
  • 5
  • 26
  • 32

1 Answers1

0

This is how I solved it currently. This sucks big time, I know. It doesn't find the current active device, instead stores the information in a text file (which has to already exist! With the current device name in it!). Yes, it's horrible. But considering my knowledge of VBScript and current knowledge of the Windows Registry, both of which are very near zero, this is the best I could come up with!

I'm posting this here, because of a lack of a simple answer anywhere else. If anybody has any better solutions, without using any other programs, I'll be very grateful to know.

Also, if anybody wants to use this code, please change the file paths and names to suit yours.

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
abhijit
  • 6,363
  • 5
  • 26
  • 32