Sams Teach Yourself Java™ in 24 Hours (Covering Java 7 and Android)

(singke) #1
ptg7068951

Streams 287

18: System.out.println(“Year: “+ id3.substring(93, 97));
19: } else{
20: System.out.println(arguments[0] + “ does not contain”
21: + “ ID3 info.”);
22: }
23: file.close();
24: } catch(Exception e) {
25: System.out.println(“Error — “+ e.toString());
26: }
27: }
28: }


Before running this class as an application, you must specify an MP3 file as
a command-line argument. The program can be run with any MP3, such as
Come On and Gettit.mp3, the unjustly forgotten 1973 soul classic by
Marion Black. If you have the song Come On and Gettit.mp3 on your
system (and you really should), Figure 20.1 shows what the ID3Reader
application displays.


LISTING 20.1 Continued


FIGURE 20.1
Runningthe ID3Readerapplication.

TIP
If you don’t haveCome On and
Gettit.mp3on your computer (a
big mistake,in my opinion),you
can look for MP3 songs to exam-
ine using the Creative Commons
license using Yahoo! Search at
http://search.yahoo.com/cc.
Creative Commons is a set of
copyright licenses that stipulate
how a work such as a song or
book can be distributed,edited,or
republished. The website Rock
Proper,at http://www.rockproper.com,
offers a collection of MP3 albums
that are licensed for sharing
under Creative Commons.

The application reads the last 128 bytes from the MP3 in Lines 10–11 of
Listing 20.1, storing them in a bytearray. This array is used in Line 12 to
create a Stringobject that contains the characters represented by those
bytes.


If the first three characters in the string are “TAG,” the MP3 file being exam-
ined contains ID3 information in a format the application understands.


In Lines 15–18, the string’s substring()method is called to display por-
tions of the string. The characters to display are from the ID3 format,
which always puts the artist, song, title, and year information in the same
positions in the last 128 bytes of an MP3 file.

Free download pdf