How to stream an MP3 file?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
RipBurn
Forum Newbie
Posts: 4
Joined: Wed Oct 30, 2002 6:55 pm

How to stream an MP3 file?

Post by RipBurn »

<newbie alert>

I have been trying to find/write some code to stream a single MP3 file when a user clicks on the play icon of the audio file they want to hear. The icon does not (and must not) provide a direct link to the .mp3 file or a .m3u file.

I have tried a number of scripts, the closest I have come to success is the following:

Code: Select all

header("Content-Disposition: inline; filename=play.m3u");
	header("Content-type: audio/mpegurl");
	header("Cache-control: private");
	readfile($content);
$content contains "http://<my domain>/<filename>.mp3"
play.m3u doesn't exist.

This causes Winamp to fire up but it gets two entries in its playlist... the first ends up causing some sync error and the 2nd is the file which finally starts streaming.

So it sort of works, but is a bit messy. As to whether I need all those 'header' lines or not, I don't know. Do I need to make a .m3u file?.. again, I'm not sure.

If someone could help me out with this it would be greatly appreciated. Thanks.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

assuming you want to code something for winamp's minibrowser, what do you want to achieve? a playlist or a mp3-streamer?
winamp is capable of retrieving the files via http on its own.
If you want to create a playlist of all mp3s in a directory you can use

Code: Select all

&lt;?php
header('Content-type: audio/x-mpegurl');
$url = 'http://'.$_SERVER&#1111;'SERVER_NAME'] . substr($_SERVER&#1111;'PHP_SELF'], 0, strrpos($_SERVER&#1111;'PHP_SELF'], '/')). '/';
$dd = opendir('.');
while( $filename = readdir($dd) )
{
	if (strrchr($filename, '.') == '.mp3')
		echo $url, rawurlencode($filename), "\n";
}
?&gt;
place this script in the directory where your mp3s are and open it in the minibrowser (via webserver)
RipBurn
Forum Newbie
Posts: 4
Joined: Wed Oct 30, 2002 6:55 pm

Post by RipBurn »

Thanks.. But I have no problem making a playlist of all the audio files in a directory. All I need is the code to start streaming a single file only. Winamp is only my preferred application, but users may use others eg. Windows Media Player.

Clicking on the 'play' link (which cannot link directly to the audio file) must start the streaming process... not downloading, as I already have that working.

Thanks.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

if a mp3-document is requested from a webserver the content-type of the response should be set to "audio/mpeg". Apache does this by default (afaik), so there's no need for an additional script.
How the client treats audio/mpeg you cannot control server-side. imho you can not offer more help than the correct mime-type.

Take a look at http://www.oddmusic.com/mp3howto.html
RipBurn
Forum Newbie
Posts: 4
Joined: Wed Oct 30, 2002 6:55 pm

Post by RipBurn »

Yes.. I agree. At present my browser, IE6, responds with the option to Open or Save when I click on any direct MP3 link. But several PHP apps I have looked at (eg, Andromeda, Zina) start a single MP3 file streaming (via Winamp on my PC) when I click on the 'Play' link. So what is being sent from the server?

Basically, all I want is to know what they do in PHP to get that to work?

Perhaps these apps construct a .m3u file on-the-fly and send that... I'm not sure. :?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

yo, take a look at Andromeda's hp
at the demo panel when you chose 'Elliott_Randall' 'Easy Ridin' Buggy'
the link is http://www.turnstyle.org/Artists/index. ... uggy%2Emp3
and the response is audio/x-mpegurl
just an advanced version of my script-snippet ;)
RipBurn
Forum Newbie
Posts: 4
Joined: Wed Oct 30, 2002 6:55 pm

Post by RipBurn »

OK.. I've had a look at Zina (Andromeda clone) and found this:

Code: Select all

header("Content-Disposition: inline; filename=playlist.m3u");
	header("Content-type: audio/mpegurl");
	header("Cache-control: private"); #IE seems to need this.
	echo $content;
The content of $content is something like:
ht2p://<domain_name>/mp3%20folder/my%20song.mp3

What seems most important is that the filename on the Content-Disposition line ends in .m3u (even though it's a fictitious name.. the file doesn't exist) My browser sees that and fires up the associated app, in this case, Winamp. If I change that to .mp3 my browser wants to Open or Save the file that is sent as I have no .mp3 association. As .m3u is more likely to be associated with "streaming" than even .mp3, I guess it's best to stick with that.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

oops, .m3u extension is a good point.
My IE/winamp combination doesn't care about it, so I forgot it :oops:
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

Here's how to stream MP3.

First of all M3U is not a dummy file. It's the extension for playlist files, and is needed to stream MP3 because it tells the player to include the file in the playlist.

All you need to do is send the name of the MP3 file to the following PHP page. You DO NOT need to include the .mp3 extension.

Just use something like this..

Code: Select all

&lt;a href="stream.php?t=themusic"&gt;Play The Music&lt;/a&gt;
and the 'stream.php' file would look like this..

Code: Select all

<?php
header("Content-Disposition: inline; filename=".$t.".m3u");
header("Content-type: audio/mpegurl"); 
header("Cache-control: private"); 
echo("http://www.yourwebsite.com/mp3s/".$t.".mp3");
?>
..this will start streaming your mp3 file.

You could always be a bit more creative and create 2 versions of the mp3. One for low-bandwidth, and one for high-bandwidth, then use something like this..

Code: Select all

&lt;a href="stream.php?t=themusic&amp;bw=low"&gt;Play The Music &#1111;56k]&lt;/a&gt;

Code: Select all

<?php
header("Content-Disposition: inline; filename=".$t."_".$bw.".m3u");
header("Content-type: audio/mpegurl"); 
header("Cache-control: private"); 
echo("http://www.yourwebsite.com/mp3s/".$t."_".$bw.".mp3");
?>

Hope this helps.
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

I'm pretty sure i've seen people stream them like this..

Code: Select all

&#1111;playlist]
File1=\path\to\mp3
NumberOfEntries=1
Version=2
Now, using some common sense...

url is something like, mp3s.php?mp3=ACDC_-_Back_In_Black.mp3

and code is:

mp3s.php

Code: Select all

<?
$mp3 = str_replace("_", " ", $_GET&#1111;'mp3']);
echo "&#1111;playlist]\n";
echo "File1=http://www.yoursite.com/mp3s/$mp3\n";
echo "NumberOfEntries=1\n";
echo "Version=2";
?>
this is how i've seen it done before.

you'll just need to figure out how to tell the browser it is a .pls file...
Post Reply