Forum Moderators: coopster

Message Too Old, No Replies

mp3 playback no longer working.

         

bhuether

1:53 am on Mar 17, 2008 (gmt 0)

10+ Year Member



I have a website with guitar lessons. Until a few days ago, audio playback worked fine. I did it simply as

header("Location: " .$link);

Every post I have ever read about using php for mp3 playback is complicated, but this simple one liner has always worked for me. Now I don't know what is going on.

I tried this code:

$mp3file = $_GET["filename"]; // e.g. /music/song.mp3 

// send content-type and length headers
header("Content-Type: audio/mpeg");
header("Content-length: ".filesize($mp3file));

// open the file and spew it back to the client
$fp = fopen($mp3file, 'rb');
fpassthru($fp);

It just echoes the contents of the file to the browser window...

I am totally confused... What do I need to do to play mp3 files again?

thanks,
brian

d40sithui

4:26 pm on Mar 17, 2008 (gmt 0)

10+ Year Member



the code you supplied should fetch the file and opens up a download dialogue window for the user to download.
both the top and bottom methods should work if you supplied the correct $link or $mp3file. I edited your code a bit to add the actual file name into the download dialogue box as well as to check for empty filenames. you may consider to further validate your filenames if you haven't done so already.


<?
//open mp3 to browser
//03-17-2008

$mp3file = $_GET["filename"]; // e.g. /music/song.mp3
if(!isset($_GET['filename']) ¦¦ empty($mp3file)){
die ("Unable to retrieve file.");
}

//echo "\$mp3file: $mp3file<br>\n";

// send content-type and length headers
header("Content-Type: audio/mpeg");
header("Content-length: ".filesize($mp3file));
header("Content-Disposition: attachment; filename=\"$mp3file\"");

//method 1
// open the file and spew it back to the client
$fp = fopen($mp3file, 'rb');
fpassthru($fp);
exit;

/*
//method 2
$file = @fopen($mp3file,"rb");
if ($file) {
while(!feof($file)) {
print(fread($file, 1024*8));
flush();
if (connection_status()!=0) {
@fclose($file);
die();
}
}
@fclose($file);
}
*/
?>

if you want to integrate the song to play within the browser, consider using the HTML <embed> tags. This is the most simple way to have the song play without having to first download it (and then opening it).
another approach that I would consider if you were to integrate music on your site is something called the XSPF player. It looks beter and has the playlist capability. I've used this a little and its pretty neat. Its written in flash, and all you need to do is supply the URLs of your music files. I think it supports several formats. [musicplayer.sourceforge.net...]