2016/07/19

Change Display with Audio output (HDTV) Easily

Changing a display is usually an easy task for normal PC user, but what about casual user or children? Also same thing with Audio Output.

I'm using a PC with a 22" inch display and it's also connected to 40" inch LED TV (as a second/Extended) Display.

Usually I let my son play PC games on the TV, so I have to change the display to TV as primary/main Display as well as audio output I have to set it to HDMI Audio, and once he finished playing I have to return it back to the PC Display.

So I thought of doing that in command prompt but unfortunately I couldn't able to do so neither for monitor/display or audio, so of course googled that where I found interesting apps that can change the Audio or the Display but not both, so I filled the gap with autoit along the Apps Display Changer II and AudioEndPointController. And here is how:

1 - Changing Audio Playback by Code/Command Prompt

 First change Audio Playback device, which is easy for non hdmi cause HDMI -for some reason- its name/number can be changed by the OS (Windows) where it can be LG TV-4 (NVIDIA High Definition Audio) or LG-SBAR-3 (NVIDIA High Definition Audio) etc ..
And All refer to the same HDMI Port where the common name is NVIDIA High Definition Audio, where this is solved using the open source app AudioEndPointController which returns list of audio device followed by a number like
Audio Device 0: LG-SBAR-4 (NVIDIA High Definition Audio)
Audio Device 1: Speakers (2- High Definition Audio Device)
Audio Device 2: Digital Audio (S/PDIF) (2- High Definition Audio Device)
So to make it dynamic with HDMI output we use the following code:
ChangeAudio.au3
#include Constants.au3
#include StringConstants.au3

Dim $syspath = "E:\MyApps\AudioEndPointController\Release\EndPointController.exe"

$DirCmd = Run(@ComSpec & " /c "&$syspath, "C:\", @SW_HIDE, $STDOUT_CHILD+$STDERR_CHILD)
Local $ResponseText
While 1
    $ResponseText &= StdoutRead($DirCmd)
    If @error Then ExitLoop
Wend

Local $NvidiaHDMI = StringRegExp($ResponseText, '.*?NVIDIA', $STR_REGEXPARRAYGLOBALMATCH)
if UBound($NvidiaHDMI) >= 1 Then
    $playbackID = StringRegExp($NvidiaHDMI[0], '\d+:', $STR_REGEXPARRAYGLOBALMATCH)
    $playbackID = StringReplace($playbackID[0],":","")
    $DirCmd = Run(@ComSpec & " /c "&$syspath& ' '& $playbackID, "C:\", @SW_HIDE, $STDOUT_CHILD+$STDERR_CHILD)
EndIf
and compile this script and name it HDMI_AudioPlayback.exe

2 - Changing the Primary Display by Code/Command Prompt

 Now we use Display Changer to get the settings for the primary display where you want to change later. the beauty of the Display Changer that it can directly get the current properties/settings of the display connected and save it to xml format (with -create option like E:\DC\dc2.exe -create="tv.xml" check examples here)

3 - Putting things together

The final code needed to change the display And Audio can be as the following:
PlayOnTV.bat
cd "E:\DC"
e:
E:\DC\dc2.exe -configure="tv.xml"
E:\MyApps\HDMI_AudioPlayback.exe
Once you want to change back the primary display to the PC:
ReturnDesktop.bat
cd "E:\MyApps\DC"
e:
E:\MyApps\DC\dc2.exe -configure="table.xml"

Links:
Autoit Sript (v3)
Display Changer II App
AudioEndPointController (GitHub)