OK, so the fab new Shoutcast 2.0 is out, but for those of you who don’t want to upgrade, or pay the licence fee you need for MP3 streaming, here’s a script to display your now playing.
This script checks to see if the current DJ is title streaming, our station has this set to “Live Default User” when no titles are being streamed. If the now playing is shown as that, then this string is hidden
If you want a demo, check here. It’s purposefully not got any CSS or anything, so it will be easier for you to theme to your own site.
The only things you should need to run this script is PHP and fsockopen allowed on the same port that your shoutcast server is on (especially if they are on different boxes)
Save this file as playing.php
<?php
//Do not put http:// in front of the IP address or the script will not work
$sc_url_ip = "YOUR SHOUTCAST IP ADDRESS";
$sc_url_port = "8000";
////////////////////
// END OF CONFIG AREA
//
// DO NOT EDIT BELOW
// UNLESS YOU KNOW WHAT YOU ARE DOING
////////////////////
function getNowPlaying($sc_url_ip,$sc_url_port)
{
$open = fsockopen($sc_url_ip,$sc_url_port,$errno,$errstr,'.5');
if ($open) {
fputs($open,"GET /7.html HTTP/1.1\nUser-Agent:Mozilla\n\n");
stream_set_timeout($open,'1');
$read = fread($open,200);
$text = explode(",",$read);
if($text[6] == '' || $text[6] == '</body></html>'){ $msg = ' live stream '; } else { $msg = $text[6]; }
$text = '<strong>Now Playing:</strong> '.$msg;
} else { return false; }
fclose($open);
return $text;
}
//////////////////
//get the now playing
$current_song = getNowPlaying($sc_url_ip,$sc_url_port);
if(preg_match('/Live - Default User/',$current_song))
echo " ";
else
print $current_song;
?>
If you don’t want to check for what’s now playing and just display it regardless, change the end of the script to
//get the now playing
$current_song = getNowPlaying($sc_url_ip,$sc_url_port);
print $current_song;
?>
OK, now for the ajax part, because we want the script to automatically update, ours is set to 10 seconds here. I’ve named this file index2.php
<script type="text/javascript">
function loadXmlHttp(url, id) {
var f = this;
f.xmlHttp = null;
/*@cc_on @*/ // used here and below, limits try/catch to those IE browsers that both benefit from and support it
/*@if(@_jscript_version >= 5)
try {f.ie = window.ActiveXObject}catch(e){f.ie = false;}
@end @*/
if (window.XMLHttpRequest&&!f.ie||/^http/.test(window.location.href))
f.xmlHttp = new XMLHttpRequest();
else if (/(object)|(function)/.test(typeof createRequest))
f.xmlHttp = createRequest();
else {
f.xmlHttp = null;
/*@cc_on @*/
/*@if(@_jscript_version >= 5)
try{f.xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");}
catch (e){try{f.xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");}catch(e){f.xmlHttp=null;}}
@end @*/
}
if(f.xmlHttp != null){
f.el = document.getElementById(id);
f.xmlHttp.open("GET",url,true);
f.xmlHttp.onreadystatechange = function(){f.stateChanged();};
f.xmlHttp.send(null);
}
}
loadXmlHttp.prototype.stateChanged=function () {
if (this.xmlHttp.readyState == 4 && (this.xmlHttp.status == 200 || !/^http/.test(window.location.href)))
this.el.innerHTML = this.xmlHttp.responseText;
}
var requestTime = function(){
new loadXmlHttp('playing.php', 'timeDiv');
setInterval(function(){new loadXmlHttp('playing.php?t=' + new Date().getTime(), 'timeDiv');}, 10000); //Time in milliseconds 10000 = 10 seconds //
}
if (window.addEventListener)
window.addEventListener('load', requestTime, false);
else if (window.attachEvent)
window.attachEvent('onload', requestTime);
</script>
</head>
<script language="javascript">
</script>
<body>
<div id="timeDiv">
</div>
So far so good!
The last part of the script, is to simple insert a require where you want the now playing to actually display, (index.php for example)
<?php
require ("./index2.php") ;
?>
And there you have it, job done!