Switching digital audio to analog when playing movie
#1
Hi. Is it possible to change the audio streming from analog to digital when watching the movie? I have two audio conections from PC:
1. Sound card to TV - analog
2. Sound card to home theater - digital via optical SPDIF
To switch to other output I must stop movie go to SYSTEM -> SETTINGS ->SYSTEM -> AUDIO OUTPUT and manually change the AUDIO OUTPUT DEVICE to Realtek Digital Output or Realtek Hight Definition Audio.

Is it possible to change the audio when watching the movie?
Reply
#2
Sure, just assign "audiotoggledigital" to one of your keys (an unused one, such as K) in a keyboard.xml (wiki) file.
Reply
#3
audiotoggledigital switching in loop ANALOG -> DIGITAL -> HDMI but audio don't switch from TV to home theater.
Audio only switch when I change the last option on the screen (audio output device). audiotoggledigital not switching this option.

Image
Reply
#4
Really there is no simple way to change the output device while watching the film?
Reply
#5
Not currently. That might change in the future, as I'm fairly certain it has been requested before (better control on audio output from a remote control, that is).
Reply
#6
You can write a simple Python script that toggles both the audio output mode and device settings from analog to HDMI and vice versa. This script can then be launched with RunScript() in a remote control key definition.

First, you'll need to make a note of your audio settings for HDMI and Analog:
  1. Using the XBMC menus, change your audio output settings to the desired values for HDMI. I'm assuming you only change the "Audio output" (mode) and "Audio output device" fields on the Audio output page.
  2. Locate and examine the contents of your guisettings.xml file located in your userdata directory.
  3. Locate the <audiooutput> stanza in the file and note the current values for the <mode> and <audiodevice> tags. The number within the <mode> tag (most likely 2) will be used in line 4 of the sample script below. The text within the <audiodevice> tag will be used in line 5 of the script.
  4. Close the guisettings.xml file.
  5. Using the XBMC menus, change your audio output settings to the desired values for Analog. Again, I'm assuming you only change the "Audio output" and "Audio output device" fields on the Audio output page.
  6. Examine the contents of your guisettings.xml file and note the changed values for the <mode> and <audiodevice> tags in the <audiooutput> stanza. The number within the <mode> tag (most likely 0) will be used in line 8 of the sample script below. The text within the <audiodevice> tag will be used in line 9 of the script.
Here is the sample Python script:
Code:
import os, xbmc
current_audio_mode = xbmc.executehttpapi( "GetGUISetting(0;audiooutput.mode)" ).replace("<li>","")
if current_audio_mode == "0":  # Current audio mode is Analog; set values for switching to HDMI
  audio_mode = 2
  audio_device = "text.from.<audiodevice>.tag.while.in.HDMI.mode"
  audio_output = "HDMI"  # Optional, if notification used
else:  # Current audio mode is not Analog (Optical/Coax or HDMI); set values for switching to Analog
  audio_mode = 0
  audio_device = "text.from.<audiodevice>.tag.while.in.Analog.mode"
  audio_output = "ANALOG"  #Optional, if notification used
# Change audio settings and display notification of new mode:
xbmc.executehttpapi( "SetGUISetting(0;audiooutput.mode;%s)" % audio_mode )
xbmc.executehttpapi( "SetGUISetting(3;audiooutput.audiodevice;%s)" % audio_device )
xbmc.executebuiltin( "Notification(Audio output changed to:, %s, 2000)" %audio_output )  # Optional notification
  • I'm assuming that the value for the guisettings.xml <audiooutput>/<mode> tag is "2" for HDMI and "0" for Analog; if that's not the case, change the values for the audio_mode variable (lines 4 and 8) to the values you noted for <audiooutput>/<mode> while in HDMI and Analog modes respectively, as explained above.
  • You'll need to substitute the text assigned to the audio_device variable (lines 5 and 9) with the text you noted in the guisettings.xml <audiooutput>/<audiodevice> tag while in HDMI and analog modes respectively, as explained above.
  • If you don't want a pop-up notification to indicate the new audio mode, delete the lines with "Optional" in the comments
  • If you're running Linux (OS X too?), be sure to give your script file executable permission.

Caveats:
  • Httpapi is deprecated in favor of JSON RPC. However, I don't know (or if there currently are) the JSON RPC equivalents for GetGUISettings and SetGUISettings; if someone does know this, please chime in.
  • If you use a Custom audio device and/or if you change any other options on the Audio output page when toggling between HDMI and Analog, you will have to revise the script accordingly.
  • If you are not familiar with Python coding, be aware that indentation of lines is functional and not merely cosmetic. Copy the line indentations from the sample script exactly as-is.

Finally, assign your script to your desired remote control button in keyboard.xml:
Code:
<some-button>XBMC.RunScript(/path/to/your-script.py)</some-button>
Note: Use backslashes ("\") for path delimiters if running Windows.
Reply
#7
Thank you very much, works great. It seems that the device name <audiodevice> is not so important. The most important was <mode> tag.
Reply
#8
Hello artrafael,

thank you for your work! I did everything like you said and get the following results:

-Notification pops up and shows the text I want it to display
-The GUI gets adjusted, meaning that former "HDMI" is now showing "Analog" and the other way round. Same applies to the output device settings.
-Actual way of output doesn't change unless you adjust it manually via System-->Audio

Maybe it's important to say that I tested this on the latest nightly build from Aug, 17th.

Can you help me on this?

Thank you in advance!
Reply
#9
Welcome to the XBMC forums.

(2012-08-18, 18:58)namelessrabbit Wrote: -Actual way of output doesn't change unless you adjust it manually via System-->Audio
What exactly do you change when you change it manually. Be very specific.

Reply
#10
I play some music to verify the successful switching of output devices (Actual amplifier connected just via analog though!) and press the key I assigned to running the script.
The notification pops up so one can tell the script has been run.
I then go to Settings-->System-->Audio Output and notice that in the GUI "Audio output" as well as "Audio output device" changed to my liking (HDMI and repectively digital output via S/PDIF).
But music keeps being played back via analog.
I then select "Audio output device" and switch it manually to analog ("DirectSound - Speakers") and after that back to my first intention which was S/PDIF. After doing so the sound output seems to change because there is no playback over analog anymore.

In other words: The GUI gets changed by the script but apparently this does not affect actual output untill you change it manually via GUI.
Reply
#11
I would verify that the text you assign to the audio_device variable for S/PDIF is exactly the same (including any spaces, punctuation, capitalization) as is assigned to <audiodevice> in guisettings.xml when you select the S/PDIF device option via the menus. Probably better to copy-and-paste the text string from guisettings.xml to your script.
Reply
#12
Now I've tested it on my running configuration with 11.0 "Eden" and it works fine, if playback is stopped before switching and started afterwards.
The next thing I could need is a script that switches output automatically when entering the "Music" section...
Thank you for your effort anyways!
Reply
#13
Hello artrafael,

since I built a HTPC for my parents I have to aim for absolute automation of everything which requires more than hitting "Play" and "Stop" ;-)
So I'd like to kindly ask for your help again.
Is it possible via python script to check which section xbmc is at currently?
I imagine something like:

if section.active == "music":
audio_mode = 0
audio_device = "my analog device"
else:
audio_mode = 2
audio_device = "my HDMI device"

But how would I initiate this? I would prefer not doing this in some kind of polling style.

Could you give me a hint?
Reply
#14
Not exactly what you are requiring, but you can write a script that does the following and assign it to a "Music" button on your remote or keyboard:
- Display Music window in XBMC
- Set audio to analog
Another script that does the following and assign it to a "Video" button:
- Display Videos window in XBMC
- Set audio to HDMI
If you also have "Movies" and "TV shows" menu items, write scripts to assign buttons for these to display the menu item and set audio to HDMI.

Some remotes already have buttons (such as the colored ones) labeled for Music, Movies, TV, etc., so it would be pretty intuitive for your parents to use these to select the desired media type and corresponding sound.

The caveat is that when you use normal menu navigation to go to "Music", "Videos", etc., the audio won't be reset. This only works when the appropriate scripts are executed (by way of your key assignments).

The scenario you describe is way beyond my knowledge and may(?) require skin or application modifications.
Reply
#15
(2012-08-12, 23:15)artrafael Wrote: You can write a simple Python script that toggles both the audio output mode and device settings from analog to HDMI and vice versa. This script can then be launched with RunScript() in a remote control key definition.

First, you'll need to make a note of your audio settings for HDMI and Analog:
  1. Using the XBMC menus, change your audio output settings to the desired values for HDMI. I'm assuming you only change the "Audio output" (mode) and "Audio output device" fields on the Audio output page.
  2. Locate and examine the contents of your guisettings.xml file located in your userdata directory.
  3. Locate the <audiooutput> stanza in the file and note the current values for the <mode> and <audiodevice> tags. The number within the <mode> tag (most likely 2) will be used in line 4 of the sample script below. The text within the <audiodevice> tag will be used in line 5 of the script.
  4. Close the guisettings.xml file.
  5. Using the XBMC menus, change your audio output settings to the desired values for Analog. Again, I'm assuming you only change the "Audio output" and "Audio output device" fields on the Audio output page.
  6. Examine the contents of your guisettings.xml file and note the changed values for the <mode> and <audiodevice> tags in the <audiooutput> stanza. The number within the <mode> tag (most likely 0) will be used in line 8 of the sample script below. The text within the <audiodevice> tag will be used in line 9 of the script.
Here is the sample Python script:
Code:
import os, xbmc
current_audio_mode = xbmc.executehttpapi( "GetGUISetting(0;audiooutput.mode)" ).replace("<li>","")
if current_audio_mode == "0":  # Current audio mode is Analog; set values for switching to HDMI
  audio_mode = 2
  audio_device = "text.from.<audiodevice>.tag.while.in.HDMI.mode"
  audio_output = "HDMI"  # Optional, if notification used
else:  # Current audio mode is not Analog (Optical/Coax or HDMI); set values for switching to Analog
  audio_mode = 0
  audio_device = "text.from.<audiodevice>.tag.while.in.Analog.mode"
  audio_output = "ANALOG"  #Optional, if notification used
# Change audio settings and display notification of new mode:
xbmc.executehttpapi( "SetGUISetting(0;audiooutput.mode;%s)" % audio_mode )
xbmc.executehttpapi( "SetGUISetting(3;audiooutput.audiodevice;%s)" % audio_device )
xbmc.executebuiltin( "Notification(Audio output changed to:, %s, 2000)" %audio_output )  # Optional notification
  • I'm assuming that the value for the guisettings.xml <audiooutput>/<mode> tag is "2" for HDMI and "0" for Analog; if that's not the case, change the values for the audio_mode variable (lines 4 and 8) to the values you noted for <audiooutput>/<mode> while in HDMI and Analog modes respectively, as explained above.
  • You'll need to substitute the text assigned to the audio_device variable (lines 5 and 9) with the text you noted in the guisettings.xml <audiooutput>/<audiodevice> tag while in HDMI and analog modes respectively, as explained above.
  • If you don't want a pop-up notification to indicate the new audio mode, delete the lines with "Optional" in the comments
  • If you're running Linux (OS X too?), be sure to give your script file executable permission.

Caveats:
  • Httpapi is deprecated in favor of JSON RPC. However, I don't know (or if there currently are) the JSON RPC equivalents for GetGUISettings and SetGUISettings; if someone does know this, please chime in.
  • If you use a Custom audio device and/or if you change any other options on the Audio output page when toggling between HDMI and Analog, you will have to revise the script accordingly.
  • If you are not familiar with Python coding, be aware that indentation of lines is functional and not merely cosmetic. Copy the line indentations from the sample script exactly as-is.

Finally, assign your script to your desired remote control button in keyboard.xml:
Code:
<some-button>XBMC.RunScript(/path/to/your-script.py)</some-button>
Note: Use backslashes ("\") for path delimiters if running Windows.

Hello artrafael,
I'd like to revisit this post. It sounds like something I'd be interested in, however I'm wondering if it could be expanded upon. I use my HDMI cable coming from the XBMC directly to my TV for 1080p picture and analog sound (through my TV speakers). I'd like to add a S/PDIF cable from my XBMC to my 5.1 receiver so I can use that for watching movies with surround sound. Is there a way to create a script similar to what you did above that would not only allow the switch from HDMI to S/PDIF, but also change my settings back and forth to include a speaker configuration of 5.1, AC3, DTS, etc...? So in other words, click a remote button and it goes from HDMI analog 2 speaker output, to S/PDIF 5.1 surround with AC3 and DTS options ticked within XBMC. Then click the same button again and it goes back to HDMI analog, 2 speaker setup with no AC3, DTS...
Reply

Logout Mark Read Team Forum Stats Members Help
Switching digital audio to analog when playing movie0