Name is required.
Email address is required.
Invalid email address
Answer is required.
Exceeding max length of 5KB

Load mp3 from an php file.


Hi guys,

I have some problems loading my mp3 file in the JW player.The mp3 file comes from a php file with the following code:

*myfile.php:*
bc.. <?php
$audio=array(
'1' => 'file1.mp3',
'2' => 'file2.mp3'
);
readfile ($audio[$_GET['mp3']]);
?>



My html code :

*test.html*

bc.. <p id="player1"><a href="http://www.macromedia.com/go/getflashplayer">Get the Flash Player</a> to see this player.</p>
<script type="text/javascript">
var s1 = new SWFObject("mediaplayer/player.swf","ply","350","350","7","#ffffff");

s1.addVariable("allowfullscreen","true");
s1.addVariable("allowscriptaccess","always");
s1.addVariable("wmode","opaque");

s1.addVariable("file", encodeURIComponent("myfile.php?mp3=1"));

s1.addVariable("type", "sound");
s1.write("player1");

</script>';


The mp3 file doesn't load.

I'm using the 4.2.90 version.

Have you an idea where is the issue and how can i fix it ?

Thanks

12 Community Answers

JW Player

User  
0 rated :


If you call your script directly from your browser, does it deliver an MP3 file?bc.. http://www.mydomain.com/path/myfile.php?mp3=1



JW Player

User  
0 rated :

When i call my script directly i have special caracters:

bc.. ID3???vTCON
,etc , that's because of the readfile php function which writes the file to the output buffer. Same with the image file.

JW Player

User  
0 rated :

That's an indication that your script is delivering an MP3 file, so it's working properly

Try setting the proper content header just before the readfile():bc.. header("Content-Type: audio/x-mp3");



or:bc.. header("Content-Type: audio/mpeg");



JW Player

User  
-1 rated :

I set the header("Content-Type: audio/mpeg");

When i call my script directly from the url, only the first
3 secondes of the mp3 are loaded !!.

I tried with the JW, the file is not loaded at all.



JW Player

User  
-1 rated :

Probably a memory limit in PHP on your host.

Try a loop:bc.. $fh = fopen($audio[$_GET['mp3']], "rb") or die("Could not open file: " . $$audio[$_GET['mp3']]);

while (!feof($fh))
{
print(fread($fh, 16384));
}

fclose($fh);

?>



JW Player

User  
0 rated :

OOps, too many $$ here:bc.. die("Could not open file: " . $$audio[$_GET['mp3']]);
*_Better_*bc.. die("Could not open file: " . $audio[$_GET['mp3']]);

JW Player

User  
0 rated :

The problem come from the type of the browser and the type of the player.

I tried the code without the loop, just with the simple readfile function. This is the results:

1- With IE, the mp3 is opened directly with Windows media player, and all the file is loaded.(Same result with your last code (The loop).
2- With FF, the mp3 is opened in the browser with Quicktime and only the 3 first second are loaded (Same result with your last code (The loop).
3. With the JW, the file is not loaded at all.(Same result with your last code (The loop).

JW Player

User  
0 rated :

Hi,

I modified my php code but the fill doesn't load:

bc.. <?php
$audio=array(
'1' => 'file.mp3'
);

# get file name
$fileName = $audio[$_GET['mp3']];
# assemble file path
$file = $fileName;
$fileSize=filesize($file);

if(isset($_GET['mp3']) && array_key_exists($_GET['mp3'],$audio))

header("Expires: Thu, 19 Nov 1981 08:52:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
header("Pragma: no-cache");
header("Content-Type: audio/mpeg");
header("Content-Disposition: attachment; filename=\"" . $fileName . "\"");
header("Content-Length: " . $fileSize);


$fh = fopen($audio[$_GET['mp3']], "rb") or die("Could not open file: " .$audio[$_GET['mp3']]);



# output file
while(!feof($fh))
{
# output file without bandwidth limiting
print(fread($fh, filesize($file)));
}
fclose($fh);

//readfile ($audio[$_GET['mp3']]);
?>



Html code:

bc.. <p id="player1"><a href="http://www.macromedia.com/go/getflashplayer">Get the Flash Player</a> to see this player.</p>
<script type="text/javascript">
var s1 = new SWFObject("mediaplayer/player.swf","ply","350","350","7","#ffffff");

s1.addParam("allowfullscreen","true");

s1.addParam("allowscriptaccess","always");

s1.addParam("wmode","opaque");

s1.addParam("file", encodeURIComponent("audio.php?mp3=1"));

s1.write("player1");

</script>





What's wrong with my code ?

JW Player

User  
0 rated :

I just spent several hours trying to fix this and eventually found a solution.

I knew that the player (javascript etc) was good because when I played an mp3 straight from the server it worked.

I also knew that my php script that served the mp3 was good because it played in my browser okay.

I knew that headers weren't the problem because, using the 'live http headers' firefox plugin I could see I had exactly re-created the same headers that worked for the mp3 coming direct from the server.

So the only posibility I could think of was that the Flash player didn't like the URL so I tried adding '&whatever=whatever.mp3' to the end of the file name and it works :-)

So to summarise, for the above example, try:

urlencode('audio.php?mp3=1&whatever=whatever.mp3');

JW Player

User  
0 rated :

Just use *type=sound*

JW Player

User  
0 rated :

i want to work i php with xampp server
how to start my project plz tell me.

Ethan Feldman

JW Player Support Agent  
0 rated :

@TEJ Bahadur – This question would be better suited for a forum about php/xampp

This question has received the maximum number of answers.