[asterisk-users] OT - How to stream a A-Law/wav file to a browser ?

Gordon Henderson gordon+asterisk at drogon.net
Thu Sep 18 11:17:35 CDT 2008


On Thu, 18 Sep 2008, Olivier wrote:

> Hi,
>
> How can I create a web page allowing people to listen (with their own PC) a
> couple of .wav/a-law files stored on a Linux server ?
> Chances are users would access this web page from Internet Explorer but if I
> could make it available to other browsers, that would be better.
>
> I googled a bit and couldn't find a tag such as media://myaudiofile.wav that
> would fulfill this spec.

If the web server is running php, then this will work:

<?

   $action = $HTTP_GET_VARS["action"] ;
   $file   = $HTTP_GET_VARS["file"] ;
   $caller = $HTTP_GET_VARS["caller"] ;

   if (empty ($action) || empty ($file))
     die ("Something went wrong")  ;

// Open the file

   $fileName = "/prefix/" . $file ;
   $fd       = @fopen ($fileName, "rb") ;

   if ($fd === FALSE)
     { Header ("Location: " . $caller . ".php?error=1") ; die () ; }

// Send the headers to the browser

   $len = filesize ($fileName) ;

   Header ("Accept-Ranges: bytes");
   Header ("Content-Length: $len") ;
   Header ("Keep-Alive: timeout=2, max=100") ;
   Header ("Connection: Keep-Alive") ;
   Header ("Content-Type: audio/x-wav") ;

   if ($action == "download")
   {
     Header ("Content-Disposition: attachment; filename=\"$fileName\"");
     Header ("Content-Description: File Transfer");
   }

// Transmit the file in 8K blocks

   while (!feof ($fd) && (connection_status () == 0))
   {
     set_time_limit (0) ;
     print (fread ($fd, 1024*8)) ;
     flush () ;
   }

   fclose ($fd) ;

?>

If this was called playback.php, then you'd reference it in other HTML 
code with:

   <a href="payback.php?action=play&file=music.wav&caller=thisFile">Click
 	here to play</a>

I've found that seems to let most browsers play (or download) most audio 
files, (most of the time ;-)

The browser will (should) do whatever it's configured to do with audio 
files. I use this to let people playback voicemail and call recordings.

Gordon



More information about the asterisk-users mailing list