I
I
ILoveYAnny2017-06-12 16:08:33
PHP
ILoveYAnny, 2017-06-12 16:08:33

Why slows down mp3 playback via radfile() on Apache?

Hello, I have Ubuntu server + Apache. If you directly upload mp3 there and request access to the file through the browser, everything is fine, everything is rewound - you click on an unloaded area, it is downloaded and played back. Rewind, everything is ok.
I have implemented an mp3 output script, so when you listen through it, the browser also opens the default player, but as soon as you click on an unloaded area or rewind, everything freezes tightly. Please tell me how can I solve this problem?
Here are the headers:

function getMedia($file, $i) {
  if (file_exists($file)) {
    if (ob_get_level()) {
     ob_end_clean();
    }
  
  $fsize = filesize($file);
  $track2play = $i.".mp3";
  $shortlen = $fsize-1;
  
  header( 'HTTP/1.1 200 OK' );
  header("Pragma: public");
  header("Expires: 0"); 
  header("Content-Type: audio/mpeg");
  header('x-amz-request-id: '.rand(0,1000));
  header('Content-Length: ' . $fsize);
  header('Content-Disposition: inline; filename="' . $track2play . '"');
  header( 'Content-Range: bytes 0-'.$shortlen.'/'.$fsize); 
  header( 'Accept-Ranges: bytes');
  header('X-Pad: avoid browser bug');
  header('Cache-Control: no-cache');
 
  readfile($file);
    exit;
  }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Aksentiev, 2017-06-12
@Sanasol

it won't work like that.
Like it or not, php is definitely not for streaming media.
If you need some kind of logic for files, you need to do this through hotlinking protection and generated links.
But the webserver should check this, not php.

S
Stalker_RED, 2017-06-12
@Stalker_RED

If you look closely at the request headers that fire when rewinding, you will find there http range. But your script does not process such headers, it always outputs the file first and in its entirety.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question