|
Posted by lawrence k on 09/03/06 04:19
Apologies that my I/O skills are so weak. I was asked to figure out a
way to stream 30 seconds of an MP3. I said I couldn't determine 30
seconds because I would not know, ahead of time, the encoding rate of
the MP3. I said I could probably play 10% of its bytes and then cut if
off, and I was told that this was acceptable. However, the code I tried
below only plays a second of the file then it cuts out. Can anyone tell
me why?
The mp3 I'm using to test this code is here:
http://www.monkeyclaus.org/Enemy.mp3
The code below is at this address:
http://www.monkeyclaus.org/streamingMedia.php
-------
$allBytesInFile = filesize("Enemy.mp3");
$tenPercentOfFileInBytes = $allBytesInFile * .1;
$tenPercentOfFileInBytes = round($tenPercentOfFileInBytes );
// 09-02-06 - this next line is giving me 282548, which is correct, I
think.
// That's how many bytes we want to share with the public.
//echo $tenPercentOfFileInBytes;
$stream = "www.monkeyclaus.org"; // put in whatever stream you want to
play
$port = "80"; // put in the port of the stream
$path = "/Enemy.mp3"; // put in any extra path, this is usually just a
/
header("Content-type: audio/mpeg");
$sock = fsockopen($stream,$port);
fputs($sock, "GET $path HTTP/1.0\r\n");
fputs($sock, "Host: $stream\r\n");
fputs($sock, "Accept: */*\r\n");
fputs($sock, "Connection: close\r\n\r\n");
// code from www.php.net, only plays a second of music before quitting
// while (!feof($sock)) {
// $buffer = fgets($sock, 4096);
// echo $buffer;
// }
for ($i=0; $i < $tenPercentOfFileInBytes;) {
$buffer = fgets($sock, 4096);
echo $buffer;
$i = $i + 4096;
}
fclose($sock);
Navigation:
[Reply to this message]
|