Self-made remote control for Kodi

Source: Internet
Author: User
Tags linux mint

Self-made remote control for Kodi

Control your home media player by running a self-made remote control on your Android phone.

Kodi is an excellent software that turns almost all computers into media centers. It can play music and videos, display pictures, and even display weather forecasts. To make it easy to use after being configured as a cinema, you can use a mobile app to access services running on XBMC machines connected to wi-fi. Many such tools can be found, covering almost all smartphone systems.

XBMC

Kodi, formerly known as XBMC, has become a history when you read this article. The project team decided to use the new name Kodi for legal reasons (because the name XBMC or X-Box Media Center references obsolete hardware that is no longer supported. However, except for the name, the others will remain unchanged. In other words, a large number of new improvements are expected in the new version. This generally does not affect remote control software. It should be able to work on existing XBMC systems and new Kodi systems.

We have already configured a Kodi System for playing music, but all the Kodi remote control we have found is not very useful, especially when the TV that is connected to the media center is not turned on. They are all a little too complicated and integrate too many functions on the cell phone's small screen. We hope to have such a system. From the very beginning, it was designed to only access music libraries and radio plug-ins, so we decided to implement one by ourselves. It does not need to use all features of Kodi, because in addition to music tasks, we can simply switch to the common Kodi remote control. Our testing system is a Raspberry Pi that has been installed with the raspbos release, but our tools are not limited by the Raspberry Pi or Kodi release, it should be able to match any Linux-based Kodi system with related plug-ins installed.

How to install Kodi14 (XBMC) in Ubuntu 14.04 and Linux Mint 17)

Recommended: Install XBMC in Ubuntu 14.04

XBMC is transplanted to Android to release daily build version

XBMC Romote: use an Android phone to control XBMC Media Playback

Installing XBMC Media Center 11.0 in Ubuntu

First, the remote control program requires a user interface. Most Kodi remote control programs are independent apps. However, we hope that users can use this music control program without installing anything. Obviously, we need to use the webpage interface. Kodi has its own webpage server, but we still use an independent webpage framework to obtain more permissions. It's okay to run more than two Web servers on the same computer, but they cannot use the same port.

Several Web frameworks are available. We chose Bottle because it is a simple and efficient framework, and we do not have any advanced features. Bottle is a Python module, so it will also be the language for writing server modules.

You should be able to find the Bottle in the package manager. In a Debian-based system (including rasmcs), you can run the following command to install it:

  1. sudo apt-get install python-bottle

The remote control program is actually only the middle layer connecting the user and the system. Bottle provides a way to interact with users, and we will use JSON APIs to interact with Kodi. This allows us to control the media player by sending a message in JSON format.

We will use a simple xbmc jason api encapsulation called xbmcjson. It is enough to send control requests without worrying about the actual JSON format and the absence of chatting with the server. It is not included in PIP package management, so you have to install it directly from GitHub:

  1. git clone https://github.com/jcsaaddupuy/python-xbmc.git
  2. cd python-xbmc
  3. sudo python setup.py install

Everything is ready, and only the code is missing.

Start with Bottle

The basic structure of our program:

  1. from xbmcjson import XBMC
  2. from bottle import route, run,template, redirect, static_file, request
  3. import os
  4. xbmc = XBMC("http://192.168.0.5/jsonrpc","xbmc","xbmc")
  5. @route('/hello/<name>')
  6. def index(name):
  7. returntemplate('
  8. run(host="0.0.0.0", port=8000)

In this way, the program will connect to Kodi (but it will not actually be used); then Bottle will begin to provide website services. In our code, it will listen to port 8000 of the host 0.0.0.0 (meaning that all hosts are allowed to connect. It only sets one site, that is,/hello/XXXX, where XXXX can be any content. No matter what XXXX is, it will be passed as the parameter name to index (). Then, replace it with the HTML webpage template.

You can first write the above content to a file (we name it remote. py), and then start it with the following command:

  1. python remote.py

Then you can access localhost: 8000/hello/world in the browser to see the effect of the template.

@ Route () is used to set the path of the Web server, and the function index () returns the data of this path. Generally, the HTML page generated by the template is returned, but this is not the only way (as will be seen later ).

Next, we will add more page entries to the application to make it a full-featured Kodi remote control, but will still adopt the same code structure.

The xbmc json api can be accessed from any computer in the same network segment as the Kodi machine. That is to say, you can develop it in your notebook and then deploy it in the media center without wasting time uploading every change.

Template-for example, the simple template in the previous example-is a method that combines Python and HTML to control the output. Theoretically, they can do a lot of things, but they will be very messy. We will only use them to generate data in the correct format. However, we have to prepare some data before getting started.

Paste

Bottle comes with a Web server. We use it to test the remote control program. However, we find that its performance is sometimes not good enough. When our remote control program is officially launched, we hope the page will be displayed more quickly. Bottle can work with many different Web servers, and we found that Paste is very good. To use this tool, you only need to install the python-paste package in the Debian system, and then modify the run call in the Code:

  1. run(host=hostname, port=hostport, server="paste")

You can find details on how to use other servers in the http://bottlepy.org/docs/dev/deployment.html.

Get data from Kodi

The xbmc json api is divided into 14 namespaces: JSONRPC, Player, Playlist, Files, AudioLibrary, VideoLibrary, Input, Application, System, Favourites, Profiles, Settings, Textures, and XBMC. Each object can be accessed through the Python XBMC object (except for Favourites, which is obviously an oversight ). Each namespace contains many methods for controlling programs. For example, Playlist. GetItems () can be used to obtain the content of a specific Playlist. The server returns data in JSON format, but the xbmcjson module converts the data to a Python dictionary.

We need to use two components in Kodi to control playback: Player and playlist. The player processes the playlist and extracts the next one from the list when each song ends. To view the currently playing content, we need to obtain the ID of the player that is currently working, and then find the ID of the current playlist based on it. This can be achieved through the following code:

  1. def get_playlistid():
  2. player = xbmc.Player.GetActivePlayers()
  3. if len(player['result'])>0:
  4. playlist_data = xbmc.Player.GetProperties({"playerid":0,"properties":["playlistid"]})
  5. if len(playlist_data['result'])>0and"playlistid"in playlist_data['result'].keys():
  6. return playlist_data['result']['playlistid']
  7. return-1

If no player is currently working (that is, the length of the result part of the returned data is 0), or the player does not process the playlist, the function returns-1. Otherwise, it returns the numeric ID of the current playlist.

When we get the ID of the current playlist, we can get the details of the list. According to our needs, there are two important points: the items in the playlist and the position of the current playback (the items that have been played are not removed from the playlist, only move the current playback position ).

  1. def get_playlist():
  2. playlistid = get_playlistid()
  3. if playlistid >=0:
  4. data = xbmc.Playlist.GetItems({"playlistid":playlistid,"properties":["title","album","artist","file"]})
  5. position_data = xbmc.Player.GetProperties({"playerid":0,'properties':["position"]})
  6. position =int(position_data['result']['position'])
  7. return data['result']['items'][position:], position
  8. return[],-1

In this way, you can return the list of the items that are being played (because we do not care about the content that has been played), and it also contains the location information used to remove the items from the list.

Image

API documentation here: http://wiki.xbmc.org /? Title = JSON-RPC_API/v6. It lists all supported functions, but the description of how to use it is a bit simple.

JSON

JSON is the abbreviation of JavaScript Object Notation. It was originally designed for serialization of JavaScript objects. It still plays this role, but it is also a good way to encode arbitrary data.

JSON objects are in the following format:

  1. {property1:value1, property2:value2, property3:value3}

Any number of attribute/value pairs are supported. For Python programmers, it looks very similar to the dictionary data structure, but these two are indeed very similar.

In the dictionary data structure, the value itself can be another JSON object or a list, so the following format is correct:

  1. {"name":"Ben","jobs":["cook","bottle-washer"],"appearance":{"height":195,"skin":"fair"}}

JSON is usually used to send and receive data in network services, and is well supported by most programming languages. If you are familiar with Python, you should be able to use the programming languages you are familiar with to call the same interface to easily control Kodi.

 

For more details, please continue to read the highlights on the next page:

  • 1
  • 2
  • Next Page

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.