-1

I have an mp3 file stored in a location outside my web root. I am able to download the file using a php script, by passing a bunch of headers. However, I want to be able to directly play it, in a little audio player embedded in my website.

$file = '/location/testfile.mp3'; //this part works fine

header("Content-Transfer-Encoding: binary"); 
header("Content-Type: audio/mpeg");
header('Content-length: ' . filesize($file));
header('Content-Disposition: inline; filename="' . basename($file) . '"');
header('X-Pad: avoid browser bug');
header('Cache-Control: no-cache');

this code is located in the tags at the top of my page, before the tag. within the body, I want to put the $file into the following code for playback:

<audio controls>
    <source src="<?php echo $file; ?>" type="audio/mp3">
    Your browser does not support the audio element.
</audio>

I see a player, which plays the sound. However, it covers the whole page. What am I doing wrong?

Amruth
  • 5,792
  • 2
  • 28
  • 41
Rob Teeuwen
  • 455
  • 5
  • 21

1 Answers1

0

I have a player which plays audio file, try this code in your PHP to render audio file:

$path = 'some/path/file.mp3';
$ch = curl_init($path);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
$data = curl_exec($ch);
curl_close($ch);
if ($data === false) {
    echo 'CURL Failed';
    exit;
}

if (preg_match('/Content-Length: (\d+)/', $data, $matches)) {
    $contentLength = (int) $matches[1];
}

header('Content-Transfer-Encoding: binary');
header('Content-Type: audio/mpeg');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Content-Length: ' . $contentLength);
ob_clean();
flush();
echo $data;
exit;
Niraj Chauhan
  • 7,677
  • 12
  • 46
  • 78
  • Hey, thanks for your reply. It returns CURL failed. I'm not sure what exactly you're doing (I only replaced your path with mine). – Rob Teeuwen Apr 01 '15 at 07:26
  • It means your server is not supporting CURL, please enable it. This might help u http://stackoverflow.com/questions/1347146/how-to-enable-curl-in-php-xampp – Niraj Chauhan Apr 01 '15 at 09:11