[Icecast] Now playing (PHP example)

larry at acbradio.org larry at acbradio.org
Tue Jan 23 17:57:25 UTC 2018


This php script is also good for displaying a now playing title on your station's website.

It is not an auto refreshing script so the page would have to be refreshed manually by the visitor when the song changes.

Script follows:

<?php 
	function getMp3StreamTitle($streamingUrl, $interval, $offset = 0, $headers = true) 
	{ 
		$needle = 'StreamTitle='; 
		$ua = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.110 Safari/537.36'; 
		$opts = [ 
			'http' => [ 
				'method' => 'GET', 
				'header' => 'Icy-MetaData: 1', 
				'user_agent' => $ua 
			] 
		]; 
		if (($headers = get_headers($streamingUrl))) 
			foreach ($headers as $h) 
				if (strpos(strtolower($h), 'icy-metaint') !== false && ($interval = explode(':', $h)[1])) 
					break; 
		$context = stream_context_create($opts); 
		if ($stream = fopen($streamingUrl, 'r', false, $context)) 
		{ 
			while($buffer = stream_get_contents($stream, $interval, $offset)) { 
				if (strpos($buffer, $needle) !== false) 
				{ 
					fclose($stream); 
					$title = explode($needle, $buffer)[1]; 
					return substr($title, 1, strpos($title, ';') - 2); 
				} 
				$offset += $interval; 
			} 
		} 
	} 
	print(getMp3StreamTitle('http://yoursite.com:port/mount', 8192)); 
?>

I use this script on the ACB Radio website at http://acbradio.org.

Each stream has its own page and this script is implemented for each stream to show what is currently playing.

So if you go to http://acbradio.org/mainstream you will see what is currently playing on that stream and all of the other information for that stream.

Larry

-----Original Message-----
From: Icecast [mailto:icecast-bounces at xiph.org] On Behalf Of Danijela Popovic
Sent: Tuesday, January 23, 2018 11:44 AM
To: Icecast streaming server user discussions <icecast at xiph.org>
Subject: Re: [Icecast] Now playing (PHP example)

Wow, thank you so much! I completely forgot about the status-json.xsl file... :D

2018-01-23 15:38 GMT+01:00, unosonic <un at aporee.org>:
> Danijela Popović:
>> Hi all,
>> Basically there's my question in the subject. I need a simple example 
>> of now playing artist, song, and listeners. Can somebody write it 
>> down for me?
>
> no time, sorry
> http://icecast.org/docs/icecast-2.4.1/server-stats.html
> 	
>> I tried so many ways to do this task, but nothing worked for me.
>
> did you try to simply fetch http://your.server:8000/status-json.xsl
> with php/curl and use json_decode method? should be easy...
>
> --u
> _______________________________________________
> Icecast mailing list
> Icecast at xiph.org
> http://lists.xiph.org/mailman/listinfo/icecast
>
_______________________________________________
Icecast mailing list
Icecast at xiph.org
http://lists.xiph.org/mailman/listinfo/icecast




More information about the Icecast mailing list