Search code examples
phpstreamingmp3shoutcast

Live audio stream valid HTTP resource


Shoutcast servers not generate valid HTTP resource. Our mobile player link (Android) requires a valid HTTP resource. How can i use php script for shoutcast stream? This script provide valid http resource? Any one knows?

<?php

$track = "shoutcaststream.mp3";

if (file_exists($track)) {
    header("Content-Type: audio/mpeg");
    header('Content-Length: ' . filesize($track));
    header('Content-Disposition: inline; filename="shoutcaststream.mp3"');
    header('X-Pad: avoid browser bug');
    header('Cache-Control: no-cache');
    readfile($track);
    exit;
} else {
    header($_SERVER['SERVER_PROTOCOL'].' 404 Not Found', true, 404);
    echo "no file";
}

Update 2

Everything now fine asper "Brad" instructions. We use set_time_limit(0) But still my stream closed after 3 mins to 5 mins? How to fix? Kindly help me.. :)


Solution

  • Yes, what you have is valid.

    The invalid part about SHOUTcast streams is that they return ICY 200 OK instead of HTTP/1.0 200 OK in their responses. If you wish to change your script to proxy the SHOUTcast request, you will likely need to connect to your SHOUTcast server via a normal TCP connection (see fsockopen()), and send the raw request data.

    Once you have a connection and are receiving data, echo this data out to your client.

    Also note that you will want to call set_time_limit(0) so that your script doesn't time out in 30 seconds or so. Also make sure to not send a Content-Length header, and set your response to be HTTP/1.0 so that you don't have to send the data as chunked. (Chunked encoding playback only works on Android 2.3 or later.)

    Finally, if you find that hacking something in place with PHP is too much of a hassle, I have a stream hosting service available for testing that does exactly what you are asking, which works fine for the built-in audio player on Android.