Back to the Antelmann.com Java Framework Tutorial

# CDDB Tutorial

The com.antelmann.cddb package provides a simple way to make use of the CDDB protocol in Java.

- under construction -
(contact info@antelmann.com for clarifying content not provided here at this time)

## Coding example

Here is an example of how the library can be used to access CD information from a CDDB server using Java:

    import com.antelmann.cddb.*;

    public class CDTest
    {
        public static void main (String args[]) throws Exception {
            CDDB cddb = new FreeDB();
            // this object gives access to a physical CD-Drive on Win32
            CDDrive drive = new CDDriveWin();
            // cdid represents a CD that is constructed from the
            // driver information of the physical CD driver object
            CDID cdid = new CDID(drive);
            // now the CDDB database is queried for entries for the given CD
            // note that there may be more than one record for a given CD.
            CDDBRecord[] records = cddb.queryCD(cdid);
            // now go through all records and retrieve the information
            for (int i = 0; i < records.length; i++) {
                System.out.println("found record: " + records[i].toString());
                // the implementation of FreeDB happens to
                // return a CDDBEntry object, so you could
                // simply cast the return value, but it's not
                // necessary.
                CDInfo info = cddb.readCDInfo(records[i]);
                // an alternative to casting is now creating
                // the CDDBEntry object based on the available info:
                CDDBEntry entry = new CDDBEntry(info.getCDDBRecord(), info.getXmcdContent());
                // now you can extract whatever info you want; e.g.:
                Track[] tracks = entry.extractTracks(true);
                for (int n = 0; n < tracks.length; n++) {
                    System.out.println(tracks[n].getTrackNo() + " " + tracks[n].getTitle());
                }
                Artist artist = entry.extractCDArtist();
                // etc. etc. ..

                // another alternative is using a CDInfo object through a parser;
                // ultimately, such a parser is used to construct all the Track
                // objects etc. in the CDDBEntry implementation
                CDDBXmcdParser parser = new CDDBXmcdParser(info.getXmcdContent());
                Track t = new Track(cdid, 0, parser.readTrackTitle(0), parser.readTrackExtension(0));
                String genre = parser.readGenre();
                // etc. etc. ..
            }
        }
    }

(c) Holger Antelmann <info@antelmann.com> - all rights reserved