Note that there are some explanatory texts on larger screens.

plurals
  1. POHTML5 Audio Pushed through PHP Not Cached
    primarykey
    data
    text
    <p>I'm writing a web audio player and i'm having issues with caching being rather unreliable and the only thing that comes to mind is the headers I'm serving.</p> <p>Due to complications in general and security I have to parse the file through PHP which is fine because it means I can create them on the fly, but the problem that has arisen is that browsers don't seem to cache the audio tracks anymore except on rare occasions, other times they may only cache the first 1/4 of the track. I can't reliably work out what it's caching or why.</p> <p>The code that serves the file is below. Just for reference, the "EncodeTrack" function only encodes the track if it hasn't already been created, it doesn't do anything if the file already exists.</p> <pre><code> ## Track Decoder and Streamer if($_GET['playtrack']) { error_log("Dec Start"); $dectimer = microtime(1); ini_set('display_errors', 0); $track = trim($_GET['playtrack']); preg_match('%^(\w+)\-(\w+)\.(\w+)$%', $track, $trackbits); $trackid = $trackbits[1]; $trackquality = $trackbits[2]; $trackformat = $trackbits[3]; $trackarr = mysql_fetch_array(mysql_query("SELECT * FROM `tracks` NATURAL JOIN `artists` NATURAL JOIN `albums` WHERE `track_hash` = '$trackid' LIMIT 1")); $file = "$musicfolder/$trackarr[artist_folder]/$trackarr[album_folder]/$trackarr[track_path]"; $trackpath = escapeshellarg($file); EncodeTrack($trackid, $trackformat, $trackpath, $trackquality); $transfile = "tmp/$trackid-$trackquality.$trackformat"; if(file_exists($transfile)) { $filesize = filesize($transfile); $file = fopen($transfile, 'r'); if($_SERVER['HTTP_RANGE']) { $partial = 1; $range = str_replace('bytes=', '', $_SERVER['HTTP_RANGE']); $range = explode(',', $range); $range = explode('-', $range[0]); if($range[0] === '') { $end = $filesize - 1; $start = $end - intval($range[0]); }elseif($range[1] === '') { $start = intval($range[0]); $end = $filesize - 1; }else{ $start = intval($range[0]); $end = intval($range[1]); if ($end &gt;= $filesize || (!$start &amp;&amp; (!$end || $end == ($filesize - 1)))) $partial = false; // Invalid range/whole file specified, return whole file } $length = $end - $start; header("HTTP/1.1 206 Partial Content", 1, 206); header("Content-Range: bytes $start-$end/$filesize"); fseek($file, $start); }else{ $length = $filesize; } $data = fread($file, $filesize); fclose($file); header("Content-Length: $length"); header("Content-Type: audio/$trackformat"); header("Accept-Ranges: bytes"); header("Expires: Tue, 23 May 2062 10:00:00 GMT"); header("ETag: $length"); header("Cache-Control: public"); echo($data); }else{ header("HTTP/1.1 404 Not Found", 1, 404); } error_log("Dec Complete - ".round((microtime(1) - $dectimer)*100)); exit; } </code></pre> <p>I'm out of ideas what I'm missing here. It may also be something I'm doing wrong in terms of the partial content handling, but as far as I can tell it's sane.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload