View Javadoc

1   package org.musicontroller.importer;
2   
3   import java.io.File;
4   import java.io.IOException;
5   
6   import org.farng.mp3.AbstractMP3Tag;
7   import org.farng.mp3.MP3File;
8   import org.farng.mp3.TagException;
9   
10  /**
11   * This MP3 inpector uses the JID3Lib SourceForge project for retrieving
12   * information from MP3 files.
13   * 
14   * @author drexler
15   * @version $Id: MP3InspectorJID3Lib.java,v 1.1 2010/03/16 18:55:43 varienaja Exp $
16   */
17  public class MP3InspectorJID3Lib implements MediafileInspector {
18  
19  	String _album;
20  	String _title;
21  	String _artist;
22  	String _genre;
23  	String _year;
24  	int _tracknr;
25  	MP3File _mp3f;
26  	
27  	/**
28  	 * Constructor for a MP3Inspector.
29  	 * 
30  	 * @param mp3file The MP3 file to be analysed.
31  	 * @throws IOException The file could not be openened.
32  	 * @throws ImporterException The file contains invalid ID3 tags.
33  	 */
34  	public MP3InspectorJID3Lib(File mp3file) throws IOException, ImporterException {
35  		initialize(mp3file);
36  	}
37  	
38  	private void initialize(File mp3file) throws IOException, ImporterException {
39  		try {
40  			 _mp3f = new MP3File(mp3file,false);
41  		} catch (TagException e) {
42  			throw new ImporterException("Invalid ID3 tags in file: "+e.getLocalizedMessage());
43  		}
44  		
45  		AbstractMP3Tag tag;
46  		if(_mp3f.hasID3v2Tag()) {
47  			tag = _mp3f.getID3v2Tag();
48  		} else {
49  			tag = _mp3f.getID3v1Tag();
50  		}
51  		if(tag==null) {
52  			// There is no ID3 tag in this file. This is OK, but it is
53  			// impossible to extract information from the file.
54  			_album = "";
55  			_artist = "";
56  			_genre = "";
57  			_year = "";
58  			_tracknr = 0;
59  		} else {
60  			_album = tag.getAlbumTitle();
61  			_title = tag.getSongTitle();
62  			_artist = tag.getLeadArtist();
63  			_genre = tag.getSongGenre();
64  			_year = tag.getYearReleased();
65  			String track;
66  			try {
67  				track = tag.getTrackNumberOnAlbum();
68  			} catch (Exception e) {
69  				track = "0";
70  			}
71  			_tracknr = TrackNumberParser.parseTrackNumber(track);
72  		}
73  	}
74  	
75  	public String getBandname() {
76  		return _artist;
77  	}
78  
79  	public String getExtension() {
80  		return ".mp3";
81  	}
82  
83  	public String getKeyword() {
84  		return _genre;
85  	}
86  
87  	public String getPlaylistname() {
88  		return _album;
89  	}
90  
91  	public int getPlaylistrow() {
92  		return _tracknr;
93  	}
94  
95  	public int getPlaylistyear() {
96  		int year = 0;
97  		try {
98  			year = Integer.parseInt(_year);
99  		} catch (NumberFormatException e) {
100 			// ignore
101 		}
102 		return year;
103 	}
104 
105 	/**
106 	 * Returns the song length in milliseconds.
107 	 */
108 	public int getSonglength() {
109 		return _mp3f.getLength();
110 	}
111 
112 	public String getSongname() {
113 		return _title;
114 	}
115 
116 	public void inspectFile(String filename) throws ImporterException, IOException {
117 		File f = new File (filename);
118 		initialize(f);
119 	}
120 
121 	public File getFile() {
122 		return _mp3f.getMp3file();
123 	}
124 	
125 
126 }