PHP Streaming Mp3

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



Leave a reply



Submit