• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2753
  • Last Modified:

Parse a m3u file

Hi,

i would like to open a m3u playlist file and then display the song name and duration in my java app.

the file structure is like:
#EXTM3U
#EXTINF:3366,Lord Of The Rings - Episode 01
K:\The Lord of the Rings BBC\The Lord Of The Rings - BBC - 01.mp3
#EXTINF:3319,J.R.R. Tolkien - The Lord of the Rings 02
K:\The Lord of the Rings BBC\The Lord Of The Rings - BBC - 02.mp3
#EXTINF:3405,J.R.R. Tolkien - The Lord of the Rings 03
K:\The Lord of the Rings BBC\The Lord Of The Rings - BBC - 03.mp3

The duration is bebetween "INF:" and ","
The song name is between "," and the "EOL"

And i want to save each of these values in side a Array.

Anybody could tell me where i could find a tutorial for this???

P.S. If i declare an Array in java without the array length,
how many elements could i put into it???

thanx
0
Knightley
Asked:
Knightley
1 Solution
 
WebstormCommented:
Hi Knightley,

>> P.S. If i declare an Array in java without the array length, how many elements could i put into it???
none because you'll get a compilation error

you can use a Vector object (see http://java.sun.com/j2se/1.4.2/docs/api/java/util/Vector.html) to store each element :

class SongElement
{
    String title,
              filepath;
    int duration;
}

To parse a m3u file, you must read it line by line, using BufferedReader (see http://java.sun.com/j2se/1.4.2/docs/api/java/io/BufferedReader.html )

Then, starting with an empty Vector
    Vector vsong=new Vector(); // java.util
you also need a SongElement variable
    SongElement current = new SongElement(); // to hold the current parsed element
then, for each line :
    if line start with "#EXTM3U", just ignore it,
    if line start with "#EXTINF:", then :
           - find the first occurence of ','    int i = s.indexOf(',');
           - update the current.duration field :     current.duration = Integer.parseInt( s.substring(8,i) );
           - update the current.title field :           current.title = s.substring(i+1); // ',' not included
    otherwise (3rd case), if s.length()>0 (non empty line), then :
           - update the current.filepath field :      current.filepath = s;
           - add this new element to the vector :  vsong.add(current);
           - create a new current element to hold the next element :   current = new SongElement();


0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now