Saturday, August 14, 2010

Playlist maker for android using SL4A(android-scripting)

NOTE: I have already created an APP that don't need android scripting installed here.

This is a simple tool for your android phone to make playlist by folder using android scripting. You need SL4A with python for android for this to work. You can download it at
http://code.google.com/p/android-scripting/
or with

and


Then just copy paste this code and run it. By default playlist is created at /music/playlist so make sure you have that folder in your sdcard. It also finds all mp3 files in your sdcard and will name the playlist by path. Here it is

import android
import os
import string
droid = android.Android()

directory = None # put list of folder to gen m3u or leave empty for all folder
m3upath = "/sdcard/music/playlist"
musicpaths = None

def createPlaylist(dir):
    files = os.listdir(dir)
    files.sort()
    songs=""
    for file in files:
        if file.find(".mp3") != -1:
            songs += file + "\n" 
    if songs != "":
        path = m3upath+"/" + dir.replace("/sdcard/","").replace("/","_") + ".m3u"
        f = open(path , "w")
        f.write(unicode(songs,"utf-8").encode("utf-8","ignore"))
        f.close()
        print "created " + path + "\n"

def listDir(dir):
    if not dir.startswith('.') and os.path.isdir(dir):
        files = os.listdir(dir)
        createPlaylist(dir)
        for f in files:
            if os.path.isdir(dir +"/"+ f):
                listDir(dir +"/"+ f)

if not os.path.exists(m3upath):
    print "error: m3u path do not exist."
else:
    dir = directory and directory or os.listdir("/sdcard/")
    for d in dir:
        listDir("/sdcard/"+ d)



I coded this in my phone cause I was too lazy adding songs one by one, unless there is already a way to do this automatically that I didn't know of.