MP3 Playlist from The Loop

378 views
Skip to first unread message

Toby Cryns

unread,
May 4, 2011, 12:21:28 AM5/4/11
to Minneapolis St. Paul WordPress User Group
Hello!
Does anyone have a recommendation on the best way to build an mp3
playlist via The Loop?

Let's say I have 10 posts, each with an mp3 attachment that I can grab
via the custom field (wp_get_attachment_url). What's the best way to
build an mp3 playlist out of these attachments? The end result would
be an mp3 player that will display a list of the mp3s and then play
through them all in succession.

I have been playing around with jPlayer (http://www.jplayer.org/), but
that has a pretty steep learning curve. Any other ideas?

Thanks!
Toby

Mitchell Hislop

unread,
May 4, 2011, 4:12:39 AM5/4/11
to mpls-stpau...@googlegroups.com
Do they have to come via the loop? I have used a few plugins to do it from a directory - why not use that directory ref in the loop and a plugin to do the playlist. 



Mitchell Hislop
Developer | Technoluster | Journalist
Facebook Twitter LinkedIn

--
You received this message because you are subscribed to the Google Groups "Minneapolis St. Paul WordPress User Group" group.
To post to this group, send email to mpls-stpau...@googlegroups.com.
To unsubscribe from this group, send email to mpls-stpaul-word...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mpls-stpaul-wordpress?hl=en.

Toby Cryns

unread,
May 4, 2011, 10:50:02 AM5/4/11
to mpls-stpau...@googlegroups.com
Mitch,
I don't think I can use a directory and here is why - the playlist will be generated on the fly based on the taxonomy archive that is being displayed.  

Here is how I have things configured: I have a taxonomy called bands.  I also have a taxonomy called albums.  So, when you click the "Metallica" link in the "bands" taxonomy, it shows all of Metallica's songs.  When the "Ride the Lightning" archive is being displayed, it shows only those Metallica songs that are on the "Ride the Lightning" album.  

Is your directory suggestion still doable given the setup above?

Toby

Garrick Van Buren

unread,
May 4, 2011, 10:54:00 AM5/4/11
to mpls-stpau...@googlegroups.com
My inclination would be to create a template that generates a .m3u (or .pls) file. A while back I did this with .ics files for my WP-iCal plugin. 

Both m3u & pls are super simple/straight forward formats. I think WordPress's enclosure functions automatically generate everything you need for it as well.

--
Garrick.

Mitchell Hislop

unread,
May 4, 2011, 11:45:44 AM5/4/11
to mpls-stpau...@googlegroups.com
Toby:

Garrick has the answer. Mine only works with a bucket of files. 

Mitchell Hislop

Nick Ciske

unread,
May 5, 2011, 12:24:19 PM5/5/11
to Minneapolis St. Paul WordPress User Group
Or just check for a query string argument in whatever template you're
using and run an alternate loop when it's found.

e.g.

?feed=m3u or ?feed=pls

if(strtolower($_GET['feed']) == 'm3u'){

// m3u loop

}else{

// Existing WP template code

}

Then you'd be able to generate that on any page that used that
template by just adding the querystring (similar to how WP does RSS
feeds).

Like Garrick said, they are very straightforward formats (basically
text files with a list of files to be played).

Here's a guide from the WinAmp forums:
http://forums.winamp.com/showthread.php?threadid=65772

_________________________
Nick Ciske
Web Design & Development | Business Consulting
http://thoughtrefinery.com/
@nciske

On May 4, 9:54 am, Garrick Van Buren <garrick.vanbu...@gmail.com>
wrote:

Toby Cryns

unread,
May 5, 2011, 11:43:32 PM5/5/11
to mpls-stpau...@googlegroups.com
Thanks for the help thus far.  I feel like I am headed in the right direction, but I am still a bit confused about how to put all the pieces together to create an online audio player.  

I can see the format that the m3u or pls files need to have (Thanks, Nick!).  Do I need to write to a txt file like this tutorial points out: http://php.about.com/od/advancedphp/ss/file_write_php.htm ?  Is there a better way to address that?  I am comfortable using the loop to build a playlist, but I haven't outputted a file via php in a few years, so I am a bit rusty there.

Also, I could not find any reference to an enclosure in my source or in my feed.  Any thoughts on where to look for that?

Will the m3u and/or pls file be playable in an online audio player?  It seems to be formatted for desktop software...  If it can be played online, do you have any recommendations on players?

Lots and lots of questions here...  I am super pumped about building this thing.  Thanks for all your help!

Toby




Garrick Van Buren

unread,
May 5, 2011, 11:54:53 PM5/5/11
to mpls-stpau...@googlegroups.com
m3u/pls is an _online_ format. If you load up a streaming audio station in iTunes, VLC, or whathaveyou, it's loading one of those formats.

Sure it could be downloaded as a file & loaded into the media player but most media players just need to be pointed at a URL (and they download it in the background)

Heck - the smart players don't even care if the format ends in .m3u or .pls - they read the file's contents.

--
Garrick.

John Havlik

unread,
May 7, 2011, 11:52:32 PM5/7/11
to Minneapolis St. Paul WordPress User Group
As long as you make sure the normal headers don't get sent (change
from mime type of text/HTML to the appropriate one for the playlist),
you should be able to just echo/sprintf() the file (both are ASCII
formats). Just note that Windows Media player is terminally stupid and
will not read the file correctly (foobar2000 and VLC have no issue
with it)... my example code (PLS format):

/*
Copyright 2011 John Havlik (email : mtekk...@gmail.com)

This program is free software; you can redistribute it and/or
modify
it under the terms of the GNU General Public License as published
by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
02110-1301 USA
*/
//Start our playlist stream, based off of three songs on my local
testbed
$songs = array(
array(
'title' => 'All of the Lights [Explicit]',
'artist' => 'Kanye West',
'duration' => '00:04:59',
'location' => 'http://atomtux/tools/05%20-%20All%20Of%20The%20Lights
%20[Explicit].mp3'
),
array(
'title' => 'Coming Home',
'artist' => 'Diddy',
'duration' => '00:03:58',
'location' => 'http://atomtux/tools/18%20-%20Coming%20Home.mp3'
),
array(
'title' => 'Erase Me',
'artist' => 'Kid Cudi',
'duration' => '00:03:13',
'location' => 'http://atomtux/tools/Kid%20Cudi%20-%20Erase%20Me.mp3'
)
);
//Need to send our proper headers so browser doesn't read as text/HTML
header('Content-type: audio/x-scpls');
//Gives the playlist a nice name rather than filename.php
header('Content-Disposition: attachment; filename="playlist.pls"');
echo "[playlist]\n";
//Add in our songs
$song_count = 1;
foreach($songs as $song)
{

echo "File$song_count=" . $song['location'] . "\n";
echo "Title$song_count=" . $song['artist'] . ' - ' . $song['title'] .
"\n";
echo "Length$song_count=" . $song['duration'] . "\n";
$song_count++;
}
//Echo the track count
echo "NumberOfEntries=$song_count\n";
//Echo our PLS version
echo "Version=2";
?>

-John Havlik

On May 5, 10:43 pm, Toby Cryns <tcr...@gmail.com> wrote:
> Thanks for the help thus far.  I feel like I am headed in the right
> direction, but I am still a bit confused about how to put all the pieces
> together to create an online audio player.
>
> I can see the format that the m3u or pls files need to have (Thanks, Nick!).
>  Do I need to write to a txt file like this tutorial points out:http://php.about.com/od/advancedphp/ss/file_write_php.htm?  Is there a
> better way to address that?  I am comfortable using the loop to build a
> playlist, but I haven't outputted a file via php in a few years, so I am a
> bit rusty there.
>
> Also, I could not find any reference to an enclosure in my source or in my
> feed.  Any thoughts on where to look for that?
>
> Will the m3u and/or pls file be playable in an *online* audio player?  It
Reply all
Reply to author
Forward
0 new messages