PHP Streaming MP3
Here's what did the trick.
$dir = dirname($_SERVER['DOCUMENT_ROOT'])."/protected_content";
$filename = $_GET['file'];
$file = $dir."/".$filename;
$extension = "mp3";
$mime_type = "audio/mpeg, audio/x-mpeg, audio/x-mpeg-3, audio/mpeg3";
if(file_exists($file)){
header('Content-type: {$mime_type}');
header('Content-length: ' . filesize($file));
header('Content-Disposition: filename="' . $filename);
header('X-Pad: avoid browser bug');
header('Cache-Control: no-cache');
readfile($file);
}else{
header("HTTP/1.0 404 Not Found");
}
read mp3 stream and echo back to client in php
file_get_contents
attempts to read a stream up to the end, and since you're trying to read from a broadcast server there will be no end.
If HTML5 is an option, the following may work.
<audio autoplay>
<source src="http://127.0.0.1:443/stream.mp3" type="audio/mpeg">
</audio>
Alternative solution:
<?php
ob_start();
header("Content-Transfer-Encoding: binary");
header("Content-Type: audio/mpeg, audio/x-mpeg, audio/x-mpeg-3, audio/mpeg3");
header('Content-Disposition: attachment; filename="stream.mp3"');
header('X-Pad: avoid browser bug');
header('Cache-Control: no-cache');
$handle = fopen("http://127.0.0.1:443/stream.mp3");
while (($data = fread($handle, $bufferSize)) { //Buffer size needs to be large enough to not break audio up while getting the next part
echo $data;
ob_flush();
flush();
set_time_limit(30); // Reset the script execution time to prevent timeouts since this page will probably never terminate.
}
Php MP3 Streaming Issues
For these kind of authenticated file serving use sendfile
method of the respected webservers behind which your php application is running, that said there are two popular webservers,
1. Apache
For Apache enable xsendfile mod
here on first read username & password using a seperate login.php
// login.php
// session_start & stop life cycle codes
username = $_POST["username"];
password = $_POST["password"];
if(hasPermission(username, password){
$_SESSION["logined"] = true; // setting a session value for successful authentication
}
// login success
Then add file php file for streaming file, say file.php
//file.php
if(isset($_SESSION["logined"])){
$filename = "uploads/path_to_file"; // absolute path to file
header('Content-type: audio/mpeg');
header('Content-Disposition: filename=' . basename($filename) );
header('X-Pad: avoid browser bug');
header('Cache-Control: no-cache');
header('X-Sendfile', $filename);
}else{
header("HTTP/1.0 404 Not Found"); // or 401 Unauthorized
}
2. Nginx
For nginx server use follow the steps described in apache section except replace X-Sendfile
with X-Accel-Redirect
, check here for more explanation related to nginx xsendfile.
php streaming or buffering mp3 file problem
OK, the problem solved. The actually problem is the Liunx system (Like CentOS and Ubuntu) do not support mp3 streaming.
To solve the problem, you have to install gnump3d to enable streaming server.
my site mu6.me can play the music like other music website now.
Related Topics
Formatting an SQL Timestamp with PHP
How to Apply a Function to an Array
Read the Session Data from Session Storage File
How to Send Soap Xml via Curl and PHP
Codeigniter - File Upload Required Validation
Simple Comet Example Using PHP and Jquery
Composer Running Out of Memory on Every Project, MAC Os X
New MySQLi(): How to Intercept an 'Unable to Connect' Error
PHP Hsv to Rgb Formula Comprehension
Twilio PHP - Ssl Certificate: Self Signed Certificate in Certificate Chain
Arabic Characters in JSON Decoding
Using Laravel Socialite to Login to Facebook
How to Reuse Deleted Primary Keys in MySQL
Using SQLite3 in PHP How to Count the Number of Rows in a Result Set
Make a Https Request Through PHP and Get Response
How to Remove the Last Comma from a String Using PHP
How to Convert Object into String in PHP
Inserting Now() into Database with Codeigniter's Active Record