I recently just upgraded my Sony Ericsson Xperia X10 phone from Android 1.6 to Android 2.1 expecting that the Mediascape application would allow me to create a playlist from the phone itself. Unfortunately I was wrong. The only way to create a playlist in Mediascape is to install a Media Go that can be downloaded from Sony PC Companion. Since I don't need another media player and I'm mostly running on Linux, installing Media Go isn't an option for me. After couple of hours googling about this issue, I found that Mediascape is able to recognize the M3U playlist file. I then went to Wikipedia to see what M3U format really is. It turns out that M3U file basically just a plain text file with some information in it and it's pretty easy to create/parse.
In order to create M3U format, there are 4 things that we need.
- Track length in seconds
- Artist name from ID3 tag
- Title name from ID3 tag
- Absolute/relative path where the audio file is located
This feature to create/read/modify the M3U format has been incorporated into my ID3Tidy application. To create a playlist in my Mediascape, all we need to do is to open the MP3 files that we wish to be included inside the playlist from /sdcard/music into the ID3Tidy application. And then create a M3U file and save it into the /sdcard/music/Whatever.m3u
Enjoy! :)
Thursday, November 18, 2010
How to Implement Groovy's File.eachLine in Java
One thing that I like from Groovy is that the closures concept. The method File.eachLine() in Groovy is indeed very useful and handy when reading a file line by line. Because of Java's lack of closures, there is a tendency to repeat the steps of reading each line of a file in every code. It is actually quite easy to implement Groovy's File.eachLine in Java.
FileFunction.java
public interface FileFunction { public void read(String line); }
M3UParserFileFunction.java
public class M3UParserFileFunction implements FileFunction { private List<File> mp3Files; private File m3uFile; public M3UParserFileFunction(File m3uFile, List<File> mp3Files) { this.mp3Files = mp3Files; this.m3uFile = m3uFile; } @Override public void read(String line) { if (!line.trim().startsWith("#")) { if (line.trim().toLowerCase().endsWith(".mp3")) { // Try relative path first, then absolute path. File file = new File(m3uFile.getParentFile(), line.trim()); if (file.exists()) { mp3Files.add(file); } else if (!file.exists()) { // Use absolute path file = new File(line.trim()); if (file.exists()) { mp3Files.add(file); } } } } } }
FileUtils.java
public class FileUtils { private FileUtils() { } public static void eachLine(File file, FileFunction fileFunction) throws IOException { BufferedReader br = null; try { br = new BufferedReader(new FileReader(file)); String line = ""; while ((line = br.readLine()) != null) { fileFunction.read(line); } } finally { if (br != null) { br.close(); } } } }
List<File> files = new ArrayList<File>(); FileUtils.eachLine(m3uFile, new M3UParserFileFunction(m3uFile, files)); for (File file : files) { doWhatever(file); }
Subscribe to:
Posts (Atom)