Calling XBMC’s (Kodi) JSON-RPC API using Python’s requests library – Pause a video

There aren’t many examples of using XBMC’s JSON-RPC API online, especially Python examples. Here is a simple example in Python, using the requests library.

Prerequisites: Make sure “Allow control of XBMC via HTTP” is set to ON in Settings -> Services -> Webserver.

The following code performs 2 requests: first, it performs an HTTP GET request to retrieve the currently playing or paused file (if any). If a result is returned, we perform a POST to the JSON-RPC to pause the currently playing file using the “playerid” returned from our GET request.

Edit – The Player.PlayPause method toggles between playing a video and pausing it. If a video is already paused, the code below will cause it to to start playing again. Please see my follow up post “XBMC’s JSON-RPC API – REALLY Pausing a video” for an example of how to only pause a video if it is already busy playing.

import requests
import json
import urllib

#Required header for XBMC JSON-RPC calls, otherwise you'll get a 
#415 HTTP response code - Unsupported media type
headers = {'content-type': 'application/json'}

#Host name where XBMC is running, leave as localhost if on this PC
#Make sure "Allow control of XBMC via HTTP" is set to ON in Settings -> 
#Services -> Webserver
xbmc_host = 'localhost'

#Configured in Settings -> Services -> Webserver -> Port
xbmc_port = 8888

#Base URL of the json RPC calls. For GET calls we append a "request" URI 
#parameter. For POSTs, we add the payload as JSON the the HTTP request body
xbmc_json_rpc_url = "http://" + xbmc_host + ":" + str(xbmc_port) + "/jsonrpc"

#Payload for the method to get the currently playing / paused video or audio
payload = {"jsonrpc": "2.0", "method": "Player.GetActivePlayers", "id": 1}
url_param = urllib.urlencode({'request': json.dumps(payload)})

response = requests.get(xbmc_json_rpc_url + '?' + url_param, 
                        headers=headers)

#response.text will look like this if something is playing
#{"id":1,"jsonrpc":"2.0","result":[{"playerid":1,"type":"video"}]}
#and if nothing is playing:
#{"id":1,"jsonrpc":"2.0","result":[]}
    
data = json.loads(response.text)
#result is an empty list if nothing is playing or paused. 
if data['result']:
    #We need the specific "playerid" of the currently playing file in order 
    #to pause it
    player_id = data['result'][0]["playerid"]

    payload = {"jsonrpc": "2.0", "method": "Player.PlayPause", 
               "params": { "playerid": player_id }, "id": 1}
    response = requests.post(xbmc_json_rpc_url, data=json.dumps(payload), 
                             headers=headers)
    
    #response.text will look like this if we're successful:
    #{"id":1,"jsonrpc":"2.0","result":{"speed":0}}

11 thoughts on “Calling XBMC’s (Kodi) JSON-RPC API using Python’s requests library – Pause a video

  1. 1:I have got a new projectt,in that i need to work on xbmc,.
    I seen your above example so simply changing this method “method”: “Player.GetActivePlayers” i can get remaining details right?

    2:And how do i get the host name,please dont mind am entirely new in this.

    3:And this url works only if the video player is on right?

  2. Hi Vishnu,

    1) Not sure what you mean here?
    2) The host name that you are using is the host name of the PC where Kodi is installed. If the script is running on the same PC as Kodi, you can use ‘localhost’ as the host name. If the PC has a static IP address on your network, then you can use the IP address as the host name.
    3) Kodi has to be running and a Video has to be playing or currently paused for the script to have an effect. If Kodi is open, but no video is playing / paused, the script will simply do nothing.

  3. Starting with XBMC JSON-RPC API v6 (Frodo/Gotham) discrete calls for pause and unpause can be done as follows (note the “play” parameter).

    PAUSE
    {“jsonrpc”: “2.0”, “method”: “Player.PlayPause”, “params”: { “playerid”: 1 , “play”: false}, “id”: 1}

    PLAY
    {“jsonrpc”: “2.0”, “method”: “Player.PlayPause”, “params”: { “playerid”: 1 , “play”: true}, “id”: 1}

  4. Hello,
    I am new to Kodi and would appreciate your help.
    My goal is to write a python script that will , create a playlist from files in a directory, play the the playlist and then quit. I am running this on Windows. here is my script below. I am getting this error at runtime: ImportError: No module named xbmc Q. how can I solve this? If I understand write it can’t find xbmc module how do I install that in Windows? Thanks a bunch

    # Autoplay videodirectory
    import os, xbmc

    # set path to dir you want to play

    path = “C:\\Video”
    dirList=os.listdir(path)

    videoList = xbmc.PlayList(xbmc.PLAYLIST_VIDEO)
    videoList.clear()

    for fname in dirList:
    videoList.add(path + “\\” + fname)

    # shuffle playlist
    #videoList.shuffle()

    # put playlist on repeat
    #xbmc.executebuiltin(“xbmc.playercontrol(RepeatAll)”)

    # play playlist
    xbmc.Player().play(videoList)

    • Hi John! Where did you get the original code from? The script is trying to import a module called “xbmc”. The module would be a python file called xbmc.py which should either be in your PYTHONPATH or installed via easy_install or pip. If you got a code example from somewhere, try to locate and download (or pip install if available) the xbmc module.

  5. Hello,

    I need to make a global Play/Pause button that will play or pause the music player.

    I made several other buttons for moving through the menu using the xbmc.Input.Up() command.

    How can I issue the xbmc.Player.PlayPause, or something like this, in order to achieve what I want?

    Thank you!

Leave a Reply

Your email address will not be published. Required fields are marked *