Switching Audio devices breaks optical audio output

Solution 1:

The trick is, that you need to switch your sink-inputs - which are only available when they are active/running:

pactl list short sink-inputs
28  8   31  protocol-native.c   float32le 2ch 48000Hz

Your stream "28" is mapped to the output "8". That output can be checked with:

pactl list short sinks
1   alsa_output.pci-0000_1e_00.3.analog-stereo  module-alsa-card.c  s16le 2ch 48000Hz   SUSPENDED
8   alsa_output.pci-0000_1c_00.1.hdmi-stereo-extra3 module-alsa-card.c  s16le 2ch 48000Hz   SUSPENDED

So currently the inputstream 28 (whatever source stream that may be) is directed to the hdmi card. If you want to change the output to the internal analog card (e.g. headphones) just can move it by issuing:

pactl move-sink-input 28 1

So one of yours sinks represent the headphones, the other the speakers. So instead of using the (" you'll nedd to switch you input to either 52 or 55.

EDIT

Since I'm not familiar with an optical device aside an analog headphone I'd try tow switch ports:

pactl list sinks
Sink #16
    State: RUNNING
    Name: alsa_output.pci-0000_1e_00.3.analog-stereo
    ....
    Ports: 
        analog-output-lineout: Line Out (type: Line, priority: 9000, available)
        analog-output-headphones: Headphones (type: Headphones, priority: 9900, available)
    Active Port: analog-output-lineout

In this case you could switch the ports in sink #16 with:

pactl set-sink-port 16 analog-output-lineout

or

pactl set-sink-port 16 analog-output-headphones

You need to find the correct sink index and the port names - that only applies if the digital output is a port.

The example is not really working by default since pulseaudio and alsa usually automute the speakers if they are inserted (see here for more information) but with the the appropriate settings it does work.