Automatically generating playlists (.m3u) from directory structure

I have many audiobooks that I like to listen to. With my growing collection, I thought it would be a good idea to set up some streaming on my internal network via a browser.

To cut a long story short, streaming is easy one file at a time – open the appropriate folder on the server to allow a web browser to browse to it. When that file is finished, click back and go to the next. Obviously this is OK for mp3 files of 30 minute episodes, but not so hot on a folder containing 40 x 5 minute tracks.

A playlist enables all the mp3 files to be played in a particular folder without any intervention. The pertinent C# code I have as a working starting point is as follows:

public void ProcessDir(string sourceDir)
StringBuilder sb = new StringBuilder();
StringBuilder sbPlayList = new StringBuilder();
List ls = new List();

string title = "";
string playListTitle = "";
string oldPlayListTitle = "";
int ct = 0;

foreach (string fileName in Directory.EnumerateFiles(sourceDir, "*.mp3", SearchOption.AllDirectories).OrderBy(filename => filename))
title = Path.GetDirectoryName(fileName).Replace(sourceDir,"");

string[] words = title.Split('\\');
if (words.Count() > 2)
playListTitle = words[2]; // the title eg. \author\title
playListTitle = words[1]; // just the authors name - no title subfolders

title = title.Replace(" ","%20");
title = title.Replace("\\", "/");
if (!ls.Contains(title))
if (ct > 0) // don't write an empty playlist on the first one
sb.AppendLine("Adding " + oldPlayListTitle + ".m3u");
StreamWriter playListFile = new StreamWriter(cbDestination.Text + oldPlayListTitle + ".m3u");
//sb.AppendLine("Contents of sbPlayList are: " + sbPlayList.ToString());
playListTitle = playListTitle.Replace(" ", "_");
oldPlayListTitle = playListTitle.ToLower();
+ title + "/"
+ Path.GetFileName(fileName).Replace(" ", "%20"));

+ title + "/"
+ Path.GetFileName(fileName).Replace(" ", "%20"));
tb.Text = sb.ToString();

I am using a VLC media plugin with Chrome to play the m3u playlists directly from a link.

With my Android phone it is slightly more complex:

  • In the Dolphin web browser click the link to the m3u.
  • Choose to download the file with ES File explorer
  • Finally open the playlist with the Just Playlists application.

I make no apologies for the brevity of the explanation – still busy testing the above. I will do a more thorough write up when I have finalised my solution.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: