Audio/video add-on tutorial

This page will outline the basics of creating your first Python add-on for XBMC. It assumes you're at least familiar with the basics of Python (or at least, programming in general). If not, we recommend visiting the Python tutorial first, and then returning here.

= Before we get started =

The first step to making an add-on is to put the basic structure in place. Make sure your directory is properly named, e.g., and that you have a properly-constructed   file. Once these are done, you can get started writing the actual code for your add-on!

= Hello, World! =

The simplest form of Python add-on is one that gets run, adds some list items, and exits to let XBMC take over the navigation; people who have written server-side code for the web should be familiar with how this works. (More complex add-ons can process user input in real-time, but we'll discuss those later.) Let's start by looking at an extremely basic example script:

While not particularly exciting, this shows the minimal amount of code you need to get an XBMC add-on up and running in Python. First, we get the add-on's process handle from. We need this to tell XBMC who we are. Next, we tell XBMC that we're going to show a list of. There are other options, like  or  ; these just change some details of how XBMC presents your add-on.

The most important part is that we create an  with the name and icon we want (  comes from the XBMC skin), set it to be a video item, and then add it to the directory. Once we're done adding items, we need to be sure to call  to let XBMC know we're done!

= Navigating between pages =

While the first example might be enough for extremely simple add-ons, most add-ons will want to have the ability to navigate between different pages. Most add-ons are designed to imitate a folder hierarchy where opening folder items shows a different list of items, and going "up" returns you to the parent folder.

Retrieving arguments
As we saw in the first example, XBMC passes some arguments to us via. This is important, since it's what will let us tailor the output on the add-on based on user input. Remember, much like a web site, each folder (or page) in an XBMC add-on is the result of a separate invocation of our script. The arguments available to us are:

Of particular interest is, which is what we'll generally use to show different things in the add-on.

Multiple content types
If your add-on provides multiple content types, e.g.  and , when the user invokes your add-on from the add-on list, XBMC will add a   parameter to your add-on's query string. For example:. This will allow you to modify the output depending on what context your add-on was invoked in.

Passing arguments
Now that we know how to retrieve the arguments passed to our add-on, we'll have to actually pass some along to it. Generally speaking, this involves creating a folder  with a URL back to your add-on; then the next invocation of the add-on will parse those arguments and do something with them. Let's look at another brief example that adds some folders to let the user navigate between different pages of the add-on:

As you can see, many parts of our code are very similar to the first example. There are a few important additions, however. First, we want to respond to the arguments supplied to our script, so we parse the query string with:

We skip the first character, since it's always. This will give us a  of  s; for example,   would be converted to. With this information, we can tailor the output of our add-on to the supplied arguments.

We also need to be able to create links back to our add-on in order to display related pages (e.g. subfolders). We can do this by creating an, passing  , and setting the URL of the item to point back to our add-on. We create the URL in the helper function, which gives us a URL that looks something like:

plugin://plugin.video.myaddon/?mode=folder&foldername=Folder+One

= Working with user settings =

Your add-on may require some configurable options for the user (e.g. login credentials for a service). These can be stored as settings. To use settings with your add-on, you'll need to make a file defining the settings in. For example:

This will allow the user to open the settings for your add-on and modify this setting as needed. Then, in your add-on, you can retrieve or set this setting with the following code:

= Showing additional metadata =

We've already seen how to show some basic metadata, like the icon image and the type of the item ( in our case). However, there are many more pieces of metadata that XBMC understands, and adding them can make life easier on your users. We'll take a look at some of the more important ones. You can also find a full list in the Python documentation for.

Icons and thumbnails
Our previous examples already showed how to set an icon, but you can also set a larger thumbnail for each list item. Both of these can be set in the  constructor:

You can also set these after creating the list item:

Fanart
Fanart (full-screen background images) are another common thing for add-ons to show. This works a bit differently from icons and thumbnails, but it's still a simple process:

If you'd prefer to show the default fanart from your add-on, you can get the path to that image from the  object:

Info labels
XBMC also lets you add useful information about each list item, like the cast, episode number, play count, and duration. The specific fields available depend on whether your list item is video, audio, or a picture. In all cases though, the format is roughly the same. For example, to add info labels to a movie, you might do the following:

For a full list of the available info labels, consult the documentation for.

Stream info
In addition to metadata about the file's contents, you can add information about the audio/video streams themselves. For a video, you might do the following:

For complete documentation about this function, see.

= Adding context menus =

In addition to the default action for a list item (e.g. playing the video), your add-on can provide additional actions for an item in the context menu. Note that this only applies to list items in your add-on. Currently, you're not able to modify the context menu for movies in the user's library. To add a context menu item, you need to give it a label and a built-in function as a string:

You can also elect to show only your context menu items: