Skip to content Skip to sidebar Skip to footer

Timing Problem For Generated Audio In Some Browsers

I need to generate audio from an mp3 file. So I use curl library to get the file, then set required headers, and echo the audio content. The problem is that it does not work correc

Solution 1:

As I guess, I was missing a php header.

Need to add the following header:

header('Accept-Ranges: bytes');

Hope that will help somebody.

UPDATE:

I found a very similar question here: HTML5 <audio> Safari live broadcast vs not

Adding the Accept-Ranges header fix the issue for chrome. But for safari you need to check for HTTP_RANGE and add Content-Range header.

Here is my implementation, which works fine in all major browsers.

$content_length = strlen($media_total);
        $total_bytes = $content_length;
        $content_length_1 = $content_length - 1;

        if (isset($_SERVER['HTTP_RANGE'])) {

            $byte_range = explode('-',trim(str_ireplace('bytes=','',$_SERVER['HTTP_RANGE'])));

            $byte_from = $byte_range[0];
            $byte_to = intval($byte_range[1]);
            $byte_to = $byte_to == 0 ? $content_length_1 : $byte_to;

            $media_total = substr($media_total,$byte_from,$byte_to);

            $content_length = strlen($media_total);

            header('HTTP/1.1 206 Partial Content');
        }
        else {
            $byte_from = 0;
            $byte_to = $content_length_1;
        }

        $content_range = 'bytes '.$byte_from.'-' . $byte_to . '/' . $total_bytes;

        header('Accept-Ranges: bytes');
        header("Content-Range: ".$content_range);
        header("Content-type: ".$type);
        header("Content-length: ".$content_length);
        header('Content-Transfer-Encoding: binary');

        echo$media_total;

        exit;

Post a Comment for "Timing Problem For Generated Audio In Some Browsers"