Jump to content

Streaming wav-File through php


sellfisch

Recommended Posts

Hello everybody,

 

i try to stream a soundfile (while recording) with an php file to a embeded(vlc plugin) media player. The Problem is, that the soundfile is still growing, so it has no final filesize and php has to send everything. My actual version is:

<?php
    header('Content-Description: File Transfer');
    header('Content-Type: audio/x-wav');
    header('Content-Disposition: attachment; filename=audio.wav');
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    ob_clean();
    flush();
    $handle = fopen("audio.wav", "r");
while (!feof($handle)) {
        $buffer = fgets($handle, 4096);
        echo $buffer;
    }
fclose($handle);
    //readfile("audio.wav");

Everything works fine if the playing offset is bigger that 10-15 secounds. Otherwise the download will be aborted (i think because php thinks that the end of file is reached). But need an offset of less than 3 secounds.

I hope there is a solution without the need of a streaming server.

 

Thanks for your help

Link to comment
https://forums.phpfreaks.com/topic/195820-streaming-wav-file-through-php/
Share on other sites

  • 3 weeks later...

You're using fgets() which looks for \n's with a maximum of 4000 bytes read before returning.  Wave files don't require \n's so this is a silly way to write the code.  If the file isn't too big, I'd suggest looking at file_get_contents() but otherwise use something like fread() to read in 4k bytes.  fgets() isn't designed for binary files anyays so it will return ASCII characters.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.