00001 package i2bpro.playlist;
00002
00003 import i2bpro.layer.Dialogs.Utils;
00004 import i2bpro.layer.Layer;
00005 import java.awt.Cursor;
00006 import java.io.BufferedReader;
00007 import java.io.BufferedWriter;
00008 import java.io.File;
00009 import java.io.FileNotFoundException;
00010 import java.io.FileReader;
00011 import java.io.FileWriter;
00012 import java.io.IOException;
00013 import java.util.HashMap;
00014 import org.jaudiotagger.audio.AudioFile;
00015 import org.jaudiotagger.audio.AudioFileIO;
00016 import org.jaudiotagger.audio.exceptions.CannotReadException;
00017 import org.jaudiotagger.audio.exceptions.CannotWriteException;
00018 import org.jaudiotagger.audio.exceptions.InvalidAudioFrameException;
00019 import org.jaudiotagger.audio.exceptions.ReadOnlyFileException;
00020 import org.jaudiotagger.tag.FieldKey;
00021 import org.jaudiotagger.tag.KeyNotFoundException;
00022 import org.jaudiotagger.tag.TagException;
00023
00024
00025
00040 public final class M3U extends basePlayList
00041 {
00052 M3U(String filename) throws FileNotFoundException, IOException,
00053 CannotReadException, TagException, ReadOnlyFileException,
00054 InvalidAudioFrameException
00055 {
00056
00057 BufferedReader f;
00058 String line;
00059 String[] tmp;
00060 String extm3u = null;
00061 f = new BufferedReader( new FileReader(filename));
00062
00063 playlist.clear();
00064 playlisttype = "m3u_1";
00065
00066 while((line = f.readLine()) != null)
00067 {
00068 tmp = line.trim().toUpperCase().split(":");
00069 if ((tmp[0].equals("$EXTM3U")) ||
00070 (tmp[0].equals("#EXTM3U")) ||
00071 (tmp[0].equals("$EXTM3U")))
00072 {
00073 playlisttype = "m3u_2";
00074 } else {
00075 if (tmp[0].equals("#EXTINF"))
00076 {
00077 extm3u = line;
00078 } else {
00079 writeToPlayList(line, extm3u);
00080 extm3u = null;
00081 }
00082 }
00083 }
00084 f.close();
00085 }
00086
00087
00088
00114 public void SavePlayList(String filename, String type, HashMap<Integer,
00115 HashMap<String, String>> list) throws IOException
00116
00117 {
00118
00119
00120 playlist = list;
00121
00122
00123
00124 boolean extm3u = false;
00125 if (type.equals("m3u_2")) extm3u = true;
00126
00127
00128 BufferedWriter f = new BufferedWriter( new FileWriter(filename));
00129 if (extm3u)
00130 {
00131
00132
00133
00134 f.write("$EXTM3U");
00135 f.newLine();
00136 }
00137
00138
00139 for(int i=0; i<playlist.size(); i++)
00140 {
00141 if (extm3u)
00142 {
00143
00144
00145 f.write((String)playlist.get(i).get("extm3u"));
00146 f.newLine();
00147 }
00148
00149
00150 f.write((String)playlist.get(i).get("media"));
00151 if (i != playlist.size() -1 ) f.newLine();
00152
00153
00154
00155
00156
00157 File file = new File((String)playlist.get(i).get("media"));
00158 try {
00159 AudioFile audiofile =AudioFileIO.read(file);
00160 audiofile.getTag().setField(FieldKey.TITLE, playlist.get(i).get("title"));
00161 audiofile.getTag().setField(FieldKey.ALBUM_ARTIST, playlist.get(i).get("album_artist"));
00162 audiofile.getTag().setField(FieldKey.ALBUM, playlist.get(i).get("album"));
00163 audiofile.getTag().setField(FieldKey.YEAR, playlist.get(i).get("year"));
00164 audiofile.getTag().setField(FieldKey.GENRE, playlist.get(i).get("genre"));
00165 audiofile.commit();
00166 }
00167 catch (CannotReadException ex) {}
00168 catch (TagException ex) {}
00169 catch (ReadOnlyFileException ex) {}
00170 catch (InvalidAudioFrameException ex) {}
00171 catch (KeyNotFoundException ex) {}
00172 catch (CannotWriteException ex) {}
00173 catch (NullPointerException ex) {}
00174 }
00175
00176
00177 f.close();
00178 }
00179
00180
00181
00198 public void InsertMedia(File[] selectedFiles) throws CannotReadException,
00199 IOException, TagException, ReadOnlyFileException,
00200 InvalidAudioFrameException
00201 {
00202
00203 String filename;
00204 String extm3u;
00205 File file;
00206
00207
00208 Cursor old_cursor = Layer.getInstance().getCursor();
00209 Layer.getInstance().setCursor(new Cursor(Cursor.WAIT_CURSOR));
00210
00211
00212
00213 for (int i=0; i<selectedFiles.length; i++)
00214 {
00215
00216 filename = selectedFiles[i].getAbsolutePath();
00217 extm3u = null;
00218 if (playlisttype.equals("m3u_2"))
00219 {
00220
00221
00222 file = new File(filename);
00223 AudioFile audiofile = AudioFileIO.read(file);
00224 extm3u = "#EXTINF:";
00225 extm3u += audiofile.getAudioHeader().getTrackLength();
00226 String title = audiofile.getTag().getFirst(FieldKey.TITLE);
00227 extm3u += ",";
00228
00229
00230
00231 extm3u += (title == null) ? file.getName() : title;
00232 }
00233
00234
00235
00236 writeToPlayList(filename, extm3u);
00237 }
00238
00239 Layer.getInstance().setCursor(old_cursor);
00240 }
00241
00242
00243
00262 protected void writeToPlayList(String line, String extm3u)
00263 throws CannotReadException, IOException, TagException,
00264 ReadOnlyFileException, InvalidAudioFrameException
00265 {
00266
00267 int i = playlist.size();
00268
00269
00270 playlist.put(i, new HashMap<String, String>());
00271
00272 String extension = Utils.getExtension(new File(line));
00273 if (extension.equals(Utils.wav))
00274 {
00275
00276
00277
00278 WavTag wavtag = new WavTag(line);
00279 playlist.get(i).putAll(wavtag.getMetaDaten());
00280 }
00281 else if (extension.equals(Utils.mp3))
00282 {
00283
00284
00285 ID3v2 id3 = new ID3v2(line);
00286 playlist.get(i).putAll(id3.getMetaDaten());
00287 }
00288
00289 playlist.get(i).put("extm3u", extm3u);
00290
00291
00292 playlist.get(i).put("media", line);
00293
00294
00295 playlist.get(i).put("index", "00:00:00");
00296 }
00297 }