How to get realtime Now Playing info from Ice Cast

icecastxspfGetting a #NowPlaying info from your Ice Cast 2 server is pretty dead simple. You may get current track information just by fetching an XSPF url from Ice Cast 2 server, unlike M3U, XSPF is an XML data that you can fetch periodically by CURL command or a simple script like php, python, or everything else you like. You may want to check www.xspf.org to know more about it. Here I'm going to explain how it works, I'm gonna use a simple jquery ajax script, a short javascript code for periodically update the ajax request and a simple php script to fetch the xspf endpoint.

  1. Locate your Ice Cast Server url, usually it will look like this http://xxx.xxx.xxx.xxx:8000/status.xsl, check your mount point, ex: /mount. So your xspf url will be http://xxx.xxx.xxx.xxx:8000/mount.xspf Screen_Shot_2014-08-12_at_11_33_32_AM
  2. Create a small function to fetch xspf endpoint for your php file, and save it to file "xspf_function.php"lang="php">function getXSPF(){ $XSPFurl = 'http://xxx.xxx.xxx.xxx:8000/mount.xspf'; $xml = simplexmlloadfile($XSPFurl); $stream['info']['title'] = (string) $xml->trackList->track->title; $stream['info']['location'] = (string) $xml->trackList->track->location;

    preg_match_all('/^(.*):(.*)/m', (string) $xml->trackList->track->annotation,$matches);
    if(isset($matches[1]) && isset($matches[2])){
        $t = array_combine($matches[1], $matches[2]);
        foreach($t as $key => $val){
            $fkey = str_replace(" ", "_",$key);
            $stream['info'][strtolower($fkey)] = trim($val);
        }
    }
    return $stream;
    

    }

  3. Setup your ajax php file, ex: current_track.phplang="php">requireonce "xspffunction.php" $stream = getXSPF(); echo $stream['info']['title'];

  4. Set your javascript periodic track updater on your current streaming websitelang="html">

The setInterval() function will periodically execute an ajax request to fetch new content from icecast xspf mount point.