1

I am playing audio in mp3 format but I want to hide the mp3 url.
I'm using PHP and I try this code but his is not working.
HTML

  <audio controls>
   <source src="mp3.php?id=1" type="audio/mpeg">
    Your browser does not support the audio element.
  </audio>   

PHP

 <?php
    $id = $_GET['id'];

  if($id == 1){
     echo '/path/to/audio.mp3';
  }

 ?>

What is wrong with this code any suggestion.
How can I do that.
Thanks...

Azeem Haider
  • 111
  • 2
  • 10

1 Answers1

0

Not tried, but this might work:

$filename = '/path/to/audio.mp3';
if(is_file($filename)) 
{
    header('Content-Type: audio/mpeg');
    header('Content-Disposition: inline;filename="'.basename($filename).'"');
    header('Content-length: '.filesize($filename));
    header('Cache-Control: no-cache');
    header("Content-Transfer-Encoding: chunked"); 
    readfile($filename);
}

Basically we are outputting the raw audio stream in the location where mp3.php is called. This is also how we do it when we say <img src='img.php'>

raidenace
  • 12,789
  • 1
  • 32
  • 35
  • is this code work for cross domain because mp3 files are in some other domain. – Azeem Haider Nov 07 '15 at 09:58
  • are you hot-linking? In any case, you should try if it works in single domain itself first and then build on top of it. You don't always get an complete off-shelf-solution, you know.. :) – raidenace Nov 07 '15 at 10:02