|
Posted by lawrence k on 09/03/06 07:37
lawrence k wrote:
> lawrence k wrote:
> > 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
> >
> > -------
> > $fileToPlay = "Enemy.mp3";
>
> I changed the code slightly since I posted the original post of this
> thread. You can see my modified code below. I'm still having the same
> problem. IE will download the file and then trigger my music player to
> play it, which is not a terrible outcome, though it would be nice if IE
> would play this file in-browser, like it does when I point IE directly
> at the MP3 file. FireFox is worse: it'll play the first second of the
> song and then stop. I suspect I'm missing some header somewhere, but I
> can't figure out which one. User-Agent, perhaps?
>
> ---
>
> $fileToPlay = "Enemy.mp3";
>
> $allBytesInFile = filesize($fileToPlay);
> $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;
>
> $streamToOpen = "www.monkeyclaus.org";
> $port = "80";
> $path = "/$fileToPlay";
>
> header("Content-type: audio/mpeg");
> $socket = fsockopen($streamToOpen,$port);
>
> fputs($socket, "GET $path HTTP/1.0\r\n");
> fputs($socket, "Host: $streamToOpen\r\n");
> fputs($socket, "Accept: */*\r\n");
> fputs($socket, "Connection: close\r\n\r\n");
>
> // while (!feof($socket)) {
> // $buffer = fgets($socket, 4096);
> // echo $buffer;
> // }
>
> $totalSize = 0;
> for ($i=0; $i < $tenPercentOfFileInBytes; ) {
> $buffer = fgets($socket);
> $strSize = strlen($buffer);
> $totalSize = $totalSize + $strSize;
> // echo "$strSize \n";
> $i = $i + $strSize;
> echo $buffer;
> }
>
> fclose($socket);
I'm still struggling to figure out why my code, above, won't work. I
added the user-agent header, but it didn't help.
Navigation:
[Reply to this message]
|