[HOW-TO] Easily switch audio outputs, settings, etc.
#1
Music 
I can't post in the how-to section so I've put it here.

A new addition to XBMC 13 is the ability to alter settings via the JSON-RPC API, making it easy to switch settings via remote controls, menu shortcuts etc. With a python script you can toggle between different groups of settings, such as audio settings, plus it uses XBMC's internal python so it should work on all OS's. A list of settings that can be modified, provided by joethefox, can be found here (note it does not contain every setting across all OS's, it is specific to Linux, but should cover the majority).

The following instruction are for Windows, so you need adapt file paths to your operating system. In your favourite text editor create a python script:
C:\Users\td\Dropbox\docs\audio_switch.py
Code:
#audio toggle script for xbmc
import xbmc
import os

tempdir = xbmc.translatePath('special://temp/')
tempfile0 = os.path.join(tempdir, 'audiooutput0')

print("CURRENT AUDIO DEVICE:")
print(xbmc.executeJSONRPC('{"jsonrpc":"2.0","method":"Settings.GetSettingValue", "params":{"setting":"audiooutput.audiodevice"},"id":1}'))

if not os.path.isfile(tempfile0):
#settings group 0
#edit below here
    xbmc.executeJSONRPC('{"jsonrpc":"2.0", "method":"Settings.SetSettingValue", "params":{"setting":"audiooutput.audiodevice", "value":"WASAPI:default"}, "id":1}')
    xbmc.executeJSONRPC('{"jsonrpc":"2.0", "method":"Settings.SetSettingValue", "params":{"setting":"audiooutput.passthrough","value":true}, "id":1}')
    xbmc.executeJSONRPC('{"jsonrpc":"2.0", "method":"Settings.SetSettingValue", "params":{"setting":"audiooutput.channels","value":8}, "id":1}')
    xbmc.executeJSONRPC('{"jsonrpc":"2.0", "method":"GUI.ShowNotification", "params":{"title":"AUDIO OUTPUT", "message":"WASAPI", "image":"C:/Users/td/AppData/Roaming/XBMC/addons/metadata.common.last.fm/icon.png"}, "id":1}')
#edit above here
    file = open(tempfile0, "a")
    file.close()
else:
#settings group 1
#edit below here
    xbmc.executeJSONRPC('{"jsonrpc":"2.0", "method":"Settings.SetSettingValue", "params":{"setting":"audiooutput.audiodevice", "value":"DIRECTSOUND:default"}, "id":1}')
    xbmc.executeJSONRPC('{"jsonrpc":"2.0", "method":"Settings.SetSettingValue", "params":{"setting":"audiooutput.passthrough","value":false}, "id":1}')
    xbmc.executeJSONRPC('{"jsonrpc":"2.0", "method":"Settings.SetSettingValue", "params":{"setting":"audiooutput.channels","value":1}, "id":1}')
    xbmc.executeJSONRPC('{"jsonrpc":"2.0", "method":"GUI.ShowNotification", "params":{"title":"AUDIO OUTPUT", "message":"DS", "image":"C:/Users/td/AppData/Roaming/XBMC/addons/metadata.common.last.fm/icon.png"}, "id":1}')        
#edit above here
    os.remove(tempfile0)

The above script toggles between two groups of settings - the audio device (The default WASAPI/DISRECTSOUND device), pass-through (true/false), number of channels (8(5.1ch)/1(2ch)) and displays a on-screen notification (link to a speaker icon to use). You need to edit the lines beginning with xbmc.executeJSONRPC and you can add/remove lines if necessary. To find current the audio output devices look in the XBMC log file after running the script (it doesn't matter if it isn't configured properly yet), it prints the current selected audio device to the log file in a JSON-RPC friendly string (search for "CURRENT AUDIO DEVICE:").

Above toggles between two groups of settings, below is an example of a script to toggle between 3 groups of settings (you can expand it for more than 3 groups):
Code:
#audio toggle script for xbmc
import xbmc
import os

tempdir = xbmc.translatePath('special://temp/')
tempfile0 = os.path.join(tempdir, 'audiooutput0')
tempfile1 = os.path.join(tempdir, 'audiooutput1')

print("CURRENT AUDIO DEVICE:")
print(xbmc.executeJSONRPC('{"jsonrpc":"2.0","method":"Settings.GetSettingValue", "params":{"setting":"audiooutput.audiodevice"},"id":1}'))

if not os.path.isfile(tempfile0):
    xbmc.executeJSONRPC('{"jsonrpc":"2.0", "method":"GUI.ShowNotification", "params":{"title":"AUDIO OUTPUT", "message":"WASAPI", "image":"C:/Users/td/AppData/Roaming/XBMC/addons/metadata.common.last.fm/icon.png"}, "id":1}')
    file = open(tempfile0, "a")
    file.close()
elif not os.path.isfile(tempfile1):
    xbmc.executeJSONRPC('{"jsonrpc":"2.0", "method":"GUI.ShowNotification", "params":{"title":"AUDIO OUTPUT", "message":"AUDIO2", "image":"C:/Users/td/AppData/Roaming/XBMC/addons/metadata.common.last.fm/icon.png"}, "id":1}')
    file = open(tempfile1, "a")
    file.close()
else:
    xbmc.executeJSONRPC('{"jsonrpc":"2.0", "method":"GUI.ShowNotification", "params":{"title":"AUDIO OUTPUT", "message":"DS", "image":"C:/Users/td/AppData/Roaming/XBMC/addons/metadata.common.last.fm/icon.png"}, "id":1}')        
    os.remove(tempfile0)
    os.remove(tempfile1)

Now to run the script within XBMC use the RunScript() function, which can be used in keyboard/remote keymaps, skin shortcuts etc:
Code:
RunScript("C:\Users\td\Dropbox\docs\audio_switch.py")


That does it, now enjoy easily switching audio outputs and settings!
Reply


Messages In This Thread
[HOW-TO] Easily switch audio outputs, settings, etc. - by teeedubb - 2014-07-09, 06:01
Logout Mark Read Team Forum Stats Members Help
[HOW-TO] Easily switch audio outputs, settings, etc.3