Page 1 of 1

PHP outputting .mp3 file for iPhone consumption.

Posted: Wed Aug 27, 2008 11:34 am
by askreet
Okay, so. I had an idea.

iPhone can't play .m3u files. Safari's QuickTime applet doesn't quite understand the concept of a playlist. So I wanted to stream internet radio. I decided to make a .php script which connected (via a socket) to a stream source and outputted a seemingly endless .mp3 file.

This worked fine on my PC, but the iPhone refused.

So I wanted to get one step simpler to prove it can be done, and I'm out of ideas.

In my web directory I created two files, test.mp3 and test.php.

Test.mp3 is a real honest-to-goodness .mp3 file of "Beast and the Harlot" by Avenged Sevenfold. Test.php looks like this:

Code: Select all

<?
 
header ("HTTP/1.1 200 OK");
$size = filesize("test.mp3");
header ("Content-Length: {$size}");
header ("Content-Type: audio/mpeg3");
 
 
$fp = fopen("test.mp3", "rb");
 
while($in = fread($fp, 4096))
{
 
echo $in;
}
 
The output file works A-OK on my PC using WinAmp, or even Quicktime! The iPhone says "This movie cannot be played." but yet, plays the .mp3 just fine.

I even tried to get real tricky and renamed it .mp3, then used an .htaccess file to set the handler for .mp3 to php5! Again, worked on my PC and not the iPhone.

I'm guessing something I'm doing is outputting a slightly different file that the PC players are okay with, but the iPhone refuses to accept.

If anyone has any ideas, it would be greatly appreciated!

I've attached the source to the stream replicator as well, since I think it's interesting. It's set to stop after 1 MB because I had a hunch that a missing or 0 Content-Length was throwing off the iPhone (not the case).

Code: Select all

<?
$debug = false;
 
if (!$debug)
{
  header("Content-Type: audio/mpeg3");
  header("Content-Length: 1048576");
}
 
if($debug) echo "Creating Socket\n";
$socket = socket_create(AF_INET, SOCK_STREAM, getprotobyname('tcp'));
 
if($debug) echo "Connecting Socket\n";
socket_connect($socket, "scfire-dll-aa05.stream.aol.com", 80);
 
if($debug) echo "Sending Request\n";
$request = "GET /stream/1048\r\n\r\n";
socket_write ( $socket, $request, strlen($request) );
 
if($debug) echo "Reading from Socket\n";
$ct = 0;
while($in = socket_read ($socket, 1024) )
{
if($debug) echo "Read 1024 bytes!\n";
if ($ct != 0) echo $in;
$ct++;
 
if($debug) {
  echo "<br><br><br>==BREAK==<br><br><br>";
  if($ct > 25) exit;
}
 
if($ct > 1024) exit;
 
}
?>