Mastodon
sungate.co.uk

sungate.co.uk

Ramblings about stuff

Now Playing on sungate.co.uk …

The observant amongst you will notice that the top of this web page includes a “My current music” area. This shows what I am currently listening to at my PC. The code for doing this was so ridiculously easy that I can share it here. The key to this one is the “song change” plugin in xmms, which is what I use. You can configure a piece of code to run whenever a new song starts playing (check the “General plugins” section of the main preferences).

This is what I have included in that section:

echo "%s" > /path/to/temp/file; 
scp /path/to/temp/file user@your.web.host:/path/to/remote/file

That puts the song name into a temporary file and uploads it to your web server, each time you change song in xmms. If your main PC is your web server, then you don’t need the ‘scp’ part.

Next, you need some code on the web server to pick up the temporary file you upload. My weblog uses PHP and I’ve used something based on the following:


$songfile='/path/to/remote/file';
$time_now = mktime(date("H"),date("i"),
  date("s"),date("m"),date("d"),date("Y"));
$last_mod = @filemtime($songfile);
echo "My current music:";
if ($time_now-3600 < $last_mod) {
  @include("$songfile");
} else {
  echo "(nothing playing right now)";
}

The code checks to see if the file is 'fresh' or not. If the uploaded song file is more than an hour old, it assumes I'm not listening to anything. Adjust to taste, add your styles etc., et voila!

One Response to Now Playing on sungate.co.uk …

  1. Ridiculously easy, and yet impressive. Congrats 🙂

    Permalink

Comments are closed.