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.
$ su Password:
$ sudo -i Password:
Then you need to install the MPD client library.
# apt-get install python-mpd Reading package lists... Done Building dependency tree Reading state information... Done The following NEW packages will be installed: python-mpd 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.
#! /usr/bin/python # # playing.py # # 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. # def _get(track): if 'artist' in track: text = track['artist'] if 'title' in track: text += ' - ' + track['title'] if 'genre' in track: text += ' (' + track['genre'] + ')' else: if 'file' in track: text = track['file'] text = text.replace("_","") if text.find("(") > 0 : text = text[0:text.find("(")] else: text = 'Unable to retrieve track information' return text # # main () # print "Content-Type: text/html\n\n" print '<html><head>' print '<title>Now Playing</title>' print '</head><body>' try: player = mpd.MPDClient() player.timeout = 10 player.connect("localhost", 6600) except: print "<p>Unable to connect.</p>" else: print # Are we playing a track? if player.status()['state'] == 'play': print '<h2>Playing</h2>' # 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': print '<h2>Paused</h2>' # Or stopped? elif player.status()['state'] == 'stop': print '<h2>Stopped</h2>' player.close() player.disconnect() print "</body></html>"
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