Having worked out how to use Python to run CGI scripts and installed Music Player I decided that I needed to combine the two so I could monitor which tracks were playing.
Before continuing make sure that you have enabled python CGI scripting and installed Music Player as described above.
Once this is done then the task on monitoring the currently playing track is made a relatively easy using the Python MPD client library which we will need to install before we can use it
Install the required packages
First you need to be running as root.
$ sudo -i
# apt-get install python-mpd
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 9,652 B of archives.
After this operation, 74.8 kB of additional disk space will be used.
Then as an example of the sort of thing that can be done I used the following python script, which I created in the httpd home folder. The first line of output (‘Content-Type: text/html\n\n’) is very important as it tells the web browser what sort of content to expect.
# Python GCI script to display some basic information including
# the currently playing track and player status.
# This program is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation, either version 3 of
# the License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public
# License along with this program.
# If not, see <http://www.gnu.org/licenses/>.
# 08 Sep 13 0.1 - Initial version - MEJT
import os, mpd, socket # os.system etc
# get(track) - Get track information.
if 'artist' in track:
text = track['artist']
if 'title' in track:
text += ' - ' + track['title']
if 'genre' in track:
text += ' (' + track['genre'] + ')'
if 'file' in track:
text = track['file']
text = text.replace("_","")
if text.find("(") > 0 : text = text[0:text.find("(")]
text = 'Unable to retrieve track information'
# main ()
print "Content-Type: text/html\n\n"
print '<title>Now Playing</title>'
player = mpd.MPDClient()
player.timeout = 10
print "<p>Unable to connect.</p>"
# Are we playing a track?
if player.status()['state'] == 'play':
# Check that there are some tracks in the playlist.
if len(player.playlistinfo()) > 0:
# Get current track details.
track = player.currentsong()
print '<p>Now : ',_get(track),'</p>'
# Is there another track in the playlist?
if int(track['pos'])+1 < len(player.playlistinfo()):
# Get next track details.
track = player.playlistinfo()[int(track['pos'])+1]
print '<p>Next : ', _get(track), '</p>'
# Are we paused?
elif player.status()['state'] == 'pause':
# Or stopped?
elif player.status()['state'] == 'stop':
Obviously this script just produces some very basic output which could be made to look a lot more impressive and it should be quite easy to modify the script to display details of additional tracks.
Perhaps listing every track in the album with the current track highlighted...
Raspberry Pi is a trademark of the Raspberry Pi Foundation