Skip to content Skip to sidebar Skip to footer

Play Php Mp3 Generated File Without Saving It Or Downloading

So i am generating mp3 file and it's working fine because when i download it i can play it just fine, but now what i want to do is output that file to my browser and play it in my

Solution 1:

Here's a way to feed up the file:

header("Content-type: audio/mpeg");
header("Content-length: " . filesize($file));
header("Cache-Control: no-cache");
header("Content-Transfer-Encoding: binary"); 
readfile($file);

Or in chunks

$total     = filesize($filepath);
$blocksize = (2 << 20); //2M chunks$sent      = 0;
$handle    = fopen($filepath, "r");

// Push headers that tell what kind of file is coming down the pike
header('Content-type: '.$content_type);
header('Content-Disposition: attachment; filename='.$filename);
header('Content-length: '.$filesize * 1024);

// Now we need to loop through the file and echo out chunks of file data// Dumping the whole file fails at > 30M!while($sent < $total){
    echo fread($handle, $blocksize);
    $sent += $blocksize;
}

exit(0);

Solution 2:

The important thing for you to do is specify the Content-Type header. What the browser (or other user-agent) does with it is up to them, not to you. The content type you are using now is incorrect. Use audio/mpeg.

The only way to get it to always play is to include a player on a web page. For that, you can use HTML5 audio tags, Flash, embed, etc.

Solution 3:

... and yet... Another more simpler approach ... I have tested this approach since the moment a Polish company by the name of Ivona, was purchased by Amazon on Jan. 24, 2013, so it is proven to work out of the box, after setting your AWS Polly credentials.

    header('Accept-Ranges: none');
    header('Content-Type: audio/mpeg');

    echo$audio;

Post a Comment for "Play Php Mp3 Generated File Without Saving It Or Downloading"