Android: Building Playlists

If you are like me and regularly copy music files to your device, you’ll know that some applications only work with playlists, especially fitness apps like Zombies, Run! and miCoach. But these apps use the media storage playlists, so you need to create the playlists in a certain way. I’ve generate a script below. It searches for any files ending in flac or mp3 (common audio formats), and then generates them into an m3u playlist. It prefixes the output with /sdcard/Music (which is commonly where music files go), but this can be changed.

Copy your music to /sdcard/Music/

Copy the script to the same folder.

Change directory into the folder.

Run this script using the line:

playlistmaker.sh .

And you will get a file ALL.m3u

if [ -f "$1.m3u" ]; then
echo Removing $1.m3u
rm "$1.m3u"
fi
OUTPLS="$1.m3u"
if [[ "$1" = "." ]]; then
OUTPLS=ALL.m3u
fi
echo Building "$OUTPLS"
find "$1" | grep -E ".flac$|.mp3$" | sort | sed -r "s/^(.\/)/\/sdcard\/Music\//" >"$OUTPLS"
SONGS=
wc -l "$OUTPLS" | awk '{print $1}'
if [[ $SONGS -eq 0 ]]; then
echo “$OUTPLS: ($SONGS songs), not keeping playlist”
rm “$OUTPLS”
else
echo “$OUTPLS: ($SONGS songs)”
fi

This script can also be tied together with a tree walker like so:

find -maxdepth 1 -type d -exec ./findMusicFiles.sh {} \;

The script at the start of this post will generate one playlist – ALL.m3u containing all the media in the current directory and below.

Using the second line of code will walk the directories in the current folder and generate one playlist per directory.