A recent project of mine called Coop Cam uses several live video streams served by an Icecast server at different mount points which works great, but I found there was no real solution to simply display how many viewers were actually watching the live streams.
I put together a basic PHP code that reads the Icecast XML stats file and retrieves the current overall viewers (or listeners as its officially known) of all available mount points.
// get the stats xml file // $output = file_get_contents('http://admin:[email protected]:8000/admin/stats'); // explode to make the magic happen // $listeners = explode('',$output); $listeners = explode('',$listeners); // output to the world // echo "Currently $listeners people are watching the live stream!";
Once you have amended the admin password, server name and port the code above will then connect to your server and read the /admin/stats XML file. From here it will literally pick out the content shown between the <listeners></listeners> tags and that then becomes the $listeners variable, simply place this wherever you want to display the amount of current viewers.
- This code may or may not work depending on if your hosting provider allows the file_get_contents function – In my case I use my own dedicated servers and it works without issue, if you have any problems I’m sure I can sort something for you!
- You can show the amount of sources, file connections and so on by amending the code to reflect the correct tags – A full list of tags can be seen by visiting the youricecastservername.com:8000/admin/stats page
- You can find a live working example of this script here or actually see it in place here
- Finally, you can download the script by clicking here