Embed music with PHP

I would like to do something like this:

<object width="300" height="42">
    <param name="src" value="file.php">
    <param name="autoplay" value="true">
    <param name="controller" value="true">
    <param name="bgcolor" value="#FF9900">
    <embed src="file.php" autostart="true" loop="false" width="300" height="42" controller="true" bgcolor="#FF9900"></embed>
    </object>

And then within file.php have something along the lines of:

<?php	
include ('music_file.mp3');
?>

When I’ve tried this, it won’t work, and I’m prompted to install a plugin (which after following the prompts says Unknown Plugin.

music_file.mp3, file.php and the file with the embed code are all in the root folder. Any ideas? :confused:

Thanks

Have you verified that code above works in standard html? Doesn’t look like it would be a php issue to me.

Yeah, I’ve checked it going straight to the mp3 file… and no problems.

What you’re doing doesn’t actually require PHP - you should just need to put music_file.mp3 as the src of the embed.

Is it that you want to choose different mp3s depending on certain conditions? If so, then you could add some code such as:


<embed src="<?php
$i = 0; // value of condition
switch($i) {
  case 1: echo "file1.mp3"; break;
  case 2: echo "file2.mp3"; break;
  default: echo "default.mp3"; break;
}
?>" autostart="true" loop="false" />

If you name your files consistently, then you may just be able to get away with something like: <embed src=“file<?php echo $i; ?>.mp3” … />

I was hoping to just conceal the filename of the mp3 through the php file. I know it is probably quite easy to get around if you’re determined, but I was just looking to make it a bit fairer (for a competition).

Inn general, forget concealing anything in HTML. Your visitors are downloading your content to their computer so it’s easy to see what’s been served and make a copy of that content.

However, in this case, you can hide the actual filename using a PHP script to serve the MP3, e.g.

<embed src=“serve.php?file=1” …/>

In your serve.php file, use something like (untested):


<?php
$file = 'mymusic' . $_GET['file'] . '.mp3';

if (file_exists($file)) {
  header("Content-Type: audio/mp3");
  header("Content-Transfer-Encoding: binary");
  header("Content-Length: ".filesize($file));
  readfile($file);
}
?>

Thanks :slight_smile:

Unfortunately, on the page with the embed code, I’m still being prompted to install a plugin (to which Firefox says “Unknown Plugin”). If I go directly to the serve.php page, I’m prompted to download the file.

Any suggestions?

Thanks :confused:

test it on ie , install quicktime it will solve the problem on ff

You would have to send the proper content-type headers. PHP defaults to text/html. Try sticking this in the top of your script:


header("Content-Type: audio/mpeg3");

ak007, already tried on IE and have Quicktime. No luck :frowning:

kyberfabrikken, using ceeb’s example I already tried header(“Content-Type: audio/mp3”); but header(“Content-Type: audio/mpeg3”); did not work either :frowning:

Sorry, I didn’t see that ceeb already suggested that.

I tried opening an mp3 in my browser, to see which headers it sends. Here goes:


HTTP/1.x 200 OK
Date: Mon, 05 Mar 2007 11:48:31 GMT
Server: Apache/2.2.4 (Win32) PHP/5.1.4 mod_apreq2-20051231/2.6.0
Last-Modified: Tue, 17 Oct 2006 16:55:22 GMT
Etag: "8698-78535d-9ec2eb58"
Accept-Ranges: bytes
Content-Length: 7885661
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: audio/mpeg

It appears that the proper header is neither audio/mp3 nor audio/mpeg3, but audio/mpeg.
I’m not sure about the Accept-Ranges header - I’ve never seen that before, but it’s a good idea to send Content-Length, like in ceeb’s example above.

Note though, that you get some overhead by serving the file through PHP this way. For a high traffic site, I would recommend a different strategy.

Okay, I’ve managed to get it working now :slight_smile:
Cheers

One last question, is there a way I can get the script to die() if the user has attempted to visit this page by entering it in the address bar for instance?

can you tell us what the issue was?

I ended up adjusting it slightly, turning the embed area into a small flash player. It’s now like:

<embed wmode=transparent src=“music.swf?url=http://path_to_php_file&autostart=true”></embed>