phil1283
phil1283

Reputation: 31

PHP how to stream a mp3 stream again

I´m trying to get data from a radio stream (MP3) from http and want to stream it out in https. this is what I tried:

<?php

define(URL, 'Mymp3stream.com');

$ch = curl_init(URL);

curl_setopt_array($ch, array(
  CURLOPT_CONNECTTIMEOUT => 60,
  CURLOPT_FOLLOWLOCATION => TRUE,
  CURLOPT_RETURNTRANSFER => TRUE, 
  CURLOPT_HEADER => TRUE
));

curl_exec($ch);

how can I get it streamed ? the browser doesn´t start any audio output. Even with Headers

header('Content-type: audio/mpeg');
header("Content-Transfer-Encoding: binary");
header("Pragma: no-cache");

there was no audio in the browser. the page is loading all the time.

I also tried to read the stream into a string buffer and echoed it out with:

$stream=fopen($url, 'rb');
 while(!feof($stream)){
   $buffer=fread($stream, 128);
   echo $buffer;
   flush();
 }
fclose($stream); 

another try was to use get_stream_content after opening the file with fopen. there was a minor success after adding the headers: In Chrome there was showed the audioplayer, but no sound started to play.

Upvotes: 2

Views: 1686

Answers (1)

VC.One
VC.One

Reputation: 15926

For your stream, using PHP's readfile(); is enough. It achieves same result as putting the radio link directly in an <audio> tag.

PHP code (save as testproxy.php) :

<?php

$file = "http://mp3channels.webradio.antenne.de/antenne";
readfile($file);

?>

HTML (use the above PHP file as source of some <audio> tag) :

<!DOCTYPE html>
<html>
<body>

<audio id="audio" controls>
  <source crossorigin="anonymous" src="testproxy.php" type="audio/mpeg">
  Your browser does not support the audio element.
</audio>

</body>
</html>

Upvotes: 2

Related Questions