ankit
ankit

Reputation: 77

Read M3U(playlist) file in java

Does anyone know of a java library that would allow me to read m3u files to get the file name and its absolute path as an array ... ?

Clarification: I would like my java program to be able to parse a winamp playlist file (.M3U) to get the name+path of the mp3 files in the playlist

Upvotes: 1

Views: 12508

Answers (3)

kayleighsdaddy
kayleighsdaddy

Reputation: 660

m3u is a regular text file that can be read line by line. Just remember the lines that start with # are comments. Here is a class I made for this very purpose. This class assumes you want only the files on the computer. If you want files on websites you will have to make some minor edits.

/*
 * Written by Edward John Sheehan III
 * Used with Permission
 */

package AppPackage;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Edward John Sheehan III
 */
public class PlayList {
    List<String> mp3;
    int next;
    public PlayList(File f){
        mp3=new ArrayList<>();
        try (BufferedReader br = new BufferedReader(new FileReader(f))) {
            String line;
            while ((line = br.readLine()) != null) {
               addMP3(line);
            }
        } catch (IOException ex) {
            System.out.println("error is reading the file");
        }
        next=-1;
    }    

    private void addMP3(String line){
        if(line==null)return;
        if(!Character.isUpperCase(line.charAt(0)))return;
        if(line.indexOf(":\\")!=1)return;
        if(line.indexOf(".mp3", line.length()-4)==-1)return;
        mp3.add(line);
    }

    public String getNext(){
        next++;
        if(mp3.size()<=next)next=0;
        return mp3.get(next);
    }
}

Then just call

Playlist = new PlayList(file);

Upvotes: 2

roomtek
roomtek

Reputation: 3767

Try my java m3u parser:

Usage:

try{
 M3U_Parser mpt = new M3U_Parser();
 M3UHolder m3hodler = mpt.parseFile(new File("12397709.m3u"));
 for (int n = 0; n < m3hodler.getSize(); n++) {
 System.out.println(m3hodler.getName(n));
 System.out.println(m3hodler.getUrl(n));
 }
}catch(Exception e){
 e.printStackTrace():
}

The project is posted here

Upvotes: 3

oksayt
oksayt

Reputation: 4365

A quick google search yields Lizzy, which seems to do what you want.

Upvotes: 4

Related Questions