Weekend Projects: Streaming Shoutcast on the iPhone

Posted on November 29th, 2008

Weekend Project

As we work on XmasRadio.ca for the launch on Monday I wanted to be sure that people can listen to the music on their iPhone. This weeks project is to make our site so that when people visit they will be automatically redirected to the streaming radio.

To setup the redirect add this to your .htaccess file…

RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} .*iPhone.*
RewriteRule ^index\.php$ iphone/index.php [L]

This will push anyone using an iPhone to a sub folder called iphone. Be sure to make an iphone folder and add the following code to allow the streaming to work…

<?php
set_time_limit(10 * 60); //this could take a while, allowing 10 minutes. just added recently see UPDATE at the top of post

$bytes_to_send = 480000 * 130; //stream about about 2 hours of music

$headers = apache_request_headers(); //get the HTTP request headers safari has sent

//if safari is only asking for a portion of the “mp3″
if (isset($headers['Range'])) {
 $exploded_range = explode(‘=’, $headers['Range']);
 $limits = explode(‘-’, $exploded_range[1]);
 $length = ($limits[1] – $limits[0]) + 1; //the content length
 $content_range = ‘bytes ‘ . $limits[0] . ‘-’ . $limits[1]; //the content range

 //send fake HTTP headers to safari, telling it that we’re sending only the portion of the “mp3″ it asked for
 header(‘HTTP/1.1 206 Partial Content’);
 header(‘Accept-Ranges: bytes’);
 header(‘Content-Length: ‘ . $length);
 header(‘Content-Range: ‘ . $content_range . ‘/’ . $bytes_to_send);
 header(‘Content-type: audio/mpeg’);

 //open the stream to the shoutcast server, set as resource $fp
 $fp = fsockopen(“shout_cast_IP”, “shout_cast_PORT”, $errno, $errstr, 30) or die(“Unable to connect to server!”);

 //HTTP commands that will initiate the shoutcast server sending stream data
 $buf = “GET / HTTP/1.0\r\nIcy-MetaData:0\r\n\r\n”;

 //send HTTP commands in string $buf to stream $fp
 fwrite($fp, $buf);
 //get next line from stream
 $buf = fgets($fp, 1024);  

 //get next few lines and discard them, this is only
 //shoutcast data that would sound like noise if iphone played them
 $buf = fgets($fp, 1024);
 $buf = fgets($fp, 1024);
 $buf = fgets($fp, 1024);
 $buf = fgets($fp, 1024);
 $buf = fgets($fp, 1024);
 $buf = fgets($fp, 1024);
 $buf = fgets($fp, 1024);
 $buf = fgets($fp, 1024);

 //break if EOF
 if ($buf == “\r\n”) {
  break;
 }

 $bytes_sent = 0;

 //while pointer is not at EOF, and not too many bytes are sent…
 while (!feof($fp) AND ($bytes_sent < $length)) {
  //read 1 byte of stream
  $buf = fread($fp, 1);

  //output byte to iphone;
  echo $buf;
  $bytes_sent++;
 }
 fclose($fp);
 exit();
}

//else, it is the initial request. safari is asking for the whole “mp3″, and seeing how big it is ($bytes_to_send)
else {
 header(‘Accept-Ranges: bytes’);
 header(‘Content-Length: ‘ . $bytes_to_send);
 header(‘Content-type: audio/mpeg’);

 echo ‘blah’;
 exit();
}
exit();
?>

This code will allow you the streaming to start automatically with buffering. You need to change the words shout_cast_IP an shout_cast_PORT in the code about to the proper IP and Port for your shoutcast server.

If you have an iPhone you can test it by going to XmasRadio.ca.

If you get stuck let me know in the comments.

Posted in Weekend Projects | | | Digg This | del.icio.us | Technorati


Related Topics:
Listen to Shoutcast on your iPhone
Weekend Projects – Make Your Site iPhone Friendly Part 2
Weekend Projects – Setting Up A Streaming Radio Station
Top 10 Weekend Projects of 2008
iPhone Friendly Domain Registration

RSS feed

2 Comments

Gravatar
Comment by JMack Subscribed to comments via email ~ come follow me on twitter @jmackhh
2008-12-01 14:50:49

I was just looking for something like this!!

my question is, im wondering is this code streaming the data to the user from shoutcast but through my webserver via http? or am i lookin at this wrong?

is the webserver just connecting the end user to the stream and handing them off or is it eating up my webhost bandwidth by streaming data through them

sorry im not a PHP guru….

Gravatar
Comment by blogadmin
2008-12-01 14:54:46

This will push the person to your shoutcast streaming server using QuickTime.

 
 

Sorry, the comment form is closed at this time.

© 2007 BlueFur Hosting | Privacy Policy
Theme by Unique Blog Designs