org.jaudiotagger.audio.AudioHeader Java Examples
The following examples show how to use
org.jaudiotagger.audio.AudioHeader.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JaudiotaggerParser.java From subsonic with GNU General Public License v3.0 | 6 votes |
/** * Parses meta data for the given music file. No guessing or reformatting is done. * * * @param file The music file to parse. * @return Meta data for the file. */ @Override public MetaData getRawMetaData(File file) { MetaData metaData = new MetaData(); try { AudioFile audioFile = AudioFileIO.read(file); Tag tag = audioFile.getTag(); if (tag != null) { metaData.setAlbumName(getTagField(tag, FieldKey.ALBUM)); metaData.setTitle(getTagField(tag, FieldKey.TITLE)); metaData.setYear(parseYear(getTagField(tag, FieldKey.YEAR))); metaData.setGenre(mapGenre(getTagField(tag, FieldKey.GENRE))); metaData.setDiscNumber(parseInteger(getTagField(tag, FieldKey.DISC_NO))); metaData.setTrackNumber(parseTrackNumber(getTagField(tag, FieldKey.TRACK))); String songArtist = getTagField(tag, FieldKey.ARTIST); String albumArtist = getTagField(tag, FieldKey.ALBUM_ARTIST); metaData.setArtist(StringUtils.isBlank(songArtist) ? albumArtist : songArtist); metaData.setAlbumArtist(StringUtils.isBlank(albumArtist) ? songArtist : albumArtist); } AudioHeader audioHeader = audioFile.getAudioHeader(); if (audioHeader != null) { metaData.setVariableBitRate(audioHeader.isVariableBitRate()); metaData.setBitRate((int) audioHeader.getBitRateAsNumber()); metaData.setDurationSeconds(audioHeader.getTrackLength()); } } catch (Throwable x) { LOG.warn("Error when parsing tags in " + file, x); } return metaData; }
Example #2
Source File: BasicPlayer.java From HubPlayer with GNU General Public License v3.0 | 6 votes |
public int getAudioTrackLength(URL url) { try { // 只能获得本地歌曲文件的信息 AudioFile file = AudioFileIO.read(new File(url.toURI())); // 获取音频文件的头信息 AudioHeader audioHeader = file.getAudioHeader(); // 文件长度 转换成时间 return audioHeader.getTrackLength(); } catch (CannotReadException | IOException | TagException | ReadOnlyFileException | InvalidAudioFrameException | URISyntaxException e) { e.printStackTrace(); return -1; } }
Example #3
Source File: JaudiotaggerParser.java From airsonic-advanced with GNU General Public License v3.0 | 4 votes |
/** * Parses meta data for the given music file. No guessing or reformatting is done. * * * @param file The music file to parse. * @return Meta data for the file. */ @Override public MetaData getRawMetaData(Path file) { MetaData metaData = new MetaData(); try { AudioFile audioFile = AudioFileIO.read(file.toFile()); Tag tag = audioFile.getTag(); if (tag != null) { metaData.setAlbumName(getTagField(tag, FieldKey.ALBUM)); metaData.setTitle(getTagField(tag, FieldKey.TITLE)); metaData.setYear(parseIntegerPattern(getTagField(tag, FieldKey.YEAR), YEAR_NUMBER_PATTERN)); metaData.setGenre(mapGenre(getTagField(tag, FieldKey.GENRE))); metaData.setDiscNumber(parseIntegerPattern(getTagField(tag, FieldKey.DISC_NO), null)); metaData.setTrackNumber(parseIntegerPattern(getTagField(tag, FieldKey.TRACK), TRACK_NUMBER_PATTERN)); metaData.setMusicBrainzReleaseId(getTagField(tag, FieldKey.MUSICBRAINZ_RELEASEID)); metaData.setMusicBrainzRecordingId(getTagField(tag, FieldKey.MUSICBRAINZ_TRACK_ID)); metaData.setArtist(getTagField(tag, FieldKey.ARTIST)); metaData.setAlbumArtist(getTagField(tag, FieldKey.ALBUM_ARTIST)); if (StringUtils.isBlank(metaData.getArtist())) { metaData.setArtist(metaData.getAlbumArtist()); } if (StringUtils.isBlank(metaData.getAlbumArtist())) { metaData.setAlbumArtist(metaData.getArtist()); } } AudioHeader audioHeader = audioFile.getAudioHeader(); if (audioHeader != null) { metaData.setVariableBitRate(audioHeader.isVariableBitRate()); metaData.setBitRate((int) audioHeader.getBitRateAsNumber()); metaData.setDuration(audioHeader.getPreciseTrackLength()); } } catch (Throwable x) { LOG.warn("Error when parsing tags in " + file, x); } return metaData; }
Example #4
Source File: SongDetailDialog.java From Music-Player with GNU General Public License v3.0 | 4 votes |
@NonNull @Override public Dialog onCreateDialog(Bundle savedInstanceState) { final Activity context = getActivity(); final Song song = getArguments().getParcelable("song"); MaterialDialog dialog = new MaterialDialog.Builder(context) .customView(R.layout.dialog_file_details, true) .title(context.getResources().getString(R.string.label_details)) .positiveText(android.R.string.ok) .build(); View dialogView = dialog.getCustomView(); final TextView fileName = dialogView.findViewById(R.id.file_name); final TextView filePath = dialogView.findViewById(R.id.file_path); final TextView fileSize = dialogView.findViewById(R.id.file_size); final TextView fileFormat = dialogView.findViewById(R.id.file_format); final TextView songLength = dialogView.findViewById(R.id.song_length); final TextView bitRate = dialogView.findViewById(R.id.bitrate); final TextView samplingRate = dialogView.findViewById(R.id.sampling_rate); fileName.setText(makeTextWithTitle(context, R.string.label_file_name, "-")); filePath.setText(makeTextWithTitle(context, R.string.label_file_path, "-")); fileSize.setText(makeTextWithTitle(context, R.string.label_file_size, "-")); fileFormat.setText(makeTextWithTitle(context, R.string.label_file_format, "-")); songLength.setText(makeTextWithTitle(context, R.string.label_song_length, "-")); bitRate.setText(makeTextWithTitle(context, R.string.label_bit_rate, "-")); samplingRate.setText(makeTextWithTitle(context, R.string.label_sampling_rate, "-")); if (song != null) { final File songFile = new File(song.data); if (songFile.exists()) { fileName.setText(makeTextWithTitle(context, R.string.label_file_name, songFile.getName())); filePath.setText(makeTextWithTitle(context, R.string.label_file_path, songFile.getAbsolutePath())); fileSize.setText(makeTextWithTitle(context, R.string.label_file_size, getFileSizeString(songFile.length()))); try { AudioFile audioFile = AudioFileIO.read(songFile); AudioHeader audioHeader = audioFile.getAudioHeader(); fileFormat.setText(makeTextWithTitle(context, R.string.label_file_format, audioHeader.getFormat())); songLength.setText(makeTextWithTitle(context, R.string.label_song_length, MusicUtil.getReadableDurationString(audioHeader.getTrackLength() * 1000))); bitRate.setText(makeTextWithTitle(context, R.string.label_bit_rate, audioHeader.getBitRate() + " kb/s")); samplingRate.setText(makeTextWithTitle(context, R.string.label_sampling_rate, audioHeader.getSampleRate() + " Hz")); } catch (@NonNull CannotReadException | IOException | TagException | ReadOnlyFileException | InvalidAudioFrameException e) { Log.e(TAG, "error while reading the song file", e); // fallback songLength.setText(makeTextWithTitle(context, R.string.label_song_length, MusicUtil.getReadableDurationString(song.duration))); } } else { // fallback fileName.setText(makeTextWithTitle(context, R.string.label_file_name, song.title)); songLength.setText(makeTextWithTitle(context, R.string.label_song_length, MusicUtil.getReadableDurationString(song.duration))); } } return dialog; }
Example #5
Source File: SongDetailDialog.java From Orin with GNU General Public License v3.0 | 4 votes |
@NonNull @Override public Dialog onCreateDialog(Bundle savedInstanceState) { final Activity context = getActivity(); final Song song = getArguments().getParcelable("song"); MaterialDialog dialog = new MaterialDialog.Builder(context) .customView(R.layout.dialog_file_details, true) .title(context.getResources().getString(R.string.label_details)) .positiveText(android.R.string.ok) .build(); View dialogView = dialog.getCustomView(); final TextView fileName = (TextView) dialogView.findViewById(R.id.file_name); final TextView filePath = (TextView) dialogView.findViewById(R.id.file_path); final TextView fileSize = (TextView) dialogView.findViewById(R.id.file_size); final TextView fileFormat = (TextView) dialogView.findViewById(R.id.file_format); final TextView trackLength = (TextView) dialogView.findViewById(R.id.track_length); final TextView bitRate = (TextView) dialogView.findViewById(R.id.bitrate); final TextView samplingRate = (TextView) dialogView.findViewById(R.id.sampling_rate); fileName.setText(makeTextWithTitle(context, R.string.label_file_name, "-")); filePath.setText(makeTextWithTitle(context, R.string.label_file_path, "-")); fileSize.setText(makeTextWithTitle(context, R.string.label_file_size, "-")); fileFormat.setText(makeTextWithTitle(context, R.string.label_file_format, "-")); trackLength.setText(makeTextWithTitle(context, R.string.label_track_length, "-")); bitRate.setText(makeTextWithTitle(context, R.string.label_bit_rate, "-")); samplingRate.setText(makeTextWithTitle(context, R.string.label_sampling_rate, "-")); try { if (song != null) { final File songFile = new File(song.data); if (songFile.exists()) { AudioFile audioFile = AudioFileIO.read(songFile); AudioHeader audioHeader = audioFile.getAudioHeader(); fileName.setText(makeTextWithTitle(context, R.string.label_file_name, songFile.getName())); filePath.setText(makeTextWithTitle(context, R.string.label_file_path, songFile.getAbsolutePath())); fileSize.setText(makeTextWithTitle(context, R.string.label_file_size, getFileSizeString(songFile.length()))); fileFormat.setText(makeTextWithTitle(context, R.string.label_file_format, audioHeader.getFormat())); trackLength.setText(makeTextWithTitle(context, R.string.label_track_length, MusicUtil.getReadableDurationString(audioHeader.getTrackLength() * 1000))); bitRate.setText(makeTextWithTitle(context, R.string.label_bit_rate, audioHeader.getBitRate() + " kb/s")); samplingRate.setText(makeTextWithTitle(context, R.string.label_sampling_rate, audioHeader.getSampleRate() + " Hz")); } } } catch (@NonNull CannotReadException | IOException | TagException | ReadOnlyFileException | InvalidAudioFrameException e) { Log.e(TAG, "error while reading the song file", e); } return dialog; }
Example #6
Source File: JaudiotaggerParser.java From airsonic with GNU General Public License v3.0 | 4 votes |
/** * Parses meta data for the given music file. No guessing or reformatting is done. * * @param file The music file to parse. * @return Meta data for the file. */ @Override public MetaData getRawMetaData(File file) { MetaData metaData = new MetaData(); try { AudioFile audioFile = AudioFileIO.read(file); Tag tag = audioFile.getTag(); if (tag != null) { metaData.setAlbumArtist(getTagField(tag, FieldKey.ALBUM_ARTIST)); metaData.setAlbumName(getTagField(tag, FieldKey.ALBUM)); metaData.setArtist(getTagField(tag, FieldKey.ARTIST)); metaData.setDiscNumber(parseInteger(getTagField(tag, FieldKey.DISC_NO))); metaData.setGenre(mapGenre(getTagField(tag, FieldKey.GENRE))); metaData.setMusicBrainzRecordingId(getTagField(tag, FieldKey.MUSICBRAINZ_TRACK_ID)); metaData.setMusicBrainzReleaseId(getTagField(tag, FieldKey.MUSICBRAINZ_RELEASEID)); metaData.setTitle(getTagField(tag, FieldKey.TITLE)); metaData.setTrackNumber(parseIntegerPattern(getTagField(tag, FieldKey.TRACK), TRACK_NUMBER_PATTERN)); metaData.setYear(parseIntegerPattern(getTagField(tag, FieldKey.YEAR), YEAR_NUMBER_PATTERN)); if (StringUtils.isBlank(metaData.getArtist())) { metaData.setArtist(metaData.getAlbumArtist()); } if (StringUtils.isBlank(metaData.getAlbumArtist())) { metaData.setAlbumArtist(metaData.getArtist()); } } AudioHeader audioHeader = audioFile.getAudioHeader(); if (audioHeader != null) { metaData.setVariableBitRate(audioHeader.isVariableBitRate()); metaData.setBitRate((int) audioHeader.getBitRateAsNumber()); metaData.setDurationSeconds(audioHeader.getTrackLength()); } } catch (Throwable x) { LOG.warn("Error when parsing tags in " + file, x); } return metaData; }
Example #7
Source File: SongDetailDialog.java From RetroMusicPlayer with GNU General Public License v3.0 | 4 votes |
@NonNull @Override public Dialog onCreateDialog(Bundle savedInstanceState) { final Activity context = getActivity(); final Song song = getArguments().getParcelable("song"); MaterialDialog dialog = new MaterialDialog.Builder(context) .customView(R.layout.dialog_file_details, true) .title(context.getResources().getString(R.string.label_details)) .positiveText(android.R.string.ok) .build(); View dialogView = dialog.getCustomView(); final TextView fileName = dialogView.findViewById(R.id.file_name); final TextView filePath = dialogView.findViewById(R.id.file_path); final TextView fileSize = dialogView.findViewById(R.id.file_size); final TextView fileFormat = dialogView.findViewById(R.id.file_format); final TextView trackLength = dialogView.findViewById(R.id.track_length); final TextView bitRate = dialogView.findViewById(R.id.bitrate); final TextView samplingRate = dialogView.findViewById(R.id.sampling_rate); fileName.setText(makeTextWithTitle(context, R.string.label_file_name, "-")); filePath.setText(makeTextWithTitle(context, R.string.label_file_path, "-")); fileSize.setText(makeTextWithTitle(context, R.string.label_file_size, "-")); fileFormat.setText(makeTextWithTitle(context, R.string.label_file_format, "-")); trackLength.setText(makeTextWithTitle(context, R.string.label_track_length, "-")); bitRate.setText(makeTextWithTitle(context, R.string.label_bit_rate, "-")); samplingRate.setText(makeTextWithTitle(context, R.string.label_sampling_rate, "-")); try { if (song != null) { final File songFile = new File(song.data); if (songFile.exists()) { AudioFile audioFile = AudioFileIO.read(songFile); AudioHeader audioHeader = audioFile.getAudioHeader(); fileName.setText(makeTextWithTitle(context, R.string.label_file_name, songFile.getName())); filePath.setText(makeTextWithTitle(context, R.string.label_file_path, songFile.getAbsolutePath())); fileSize.setText(makeTextWithTitle(context, R.string.label_file_size, getFileSizeString(songFile.length()))); fileFormat.setText(makeTextWithTitle(context, R.string.label_file_format, audioHeader.getFormat())); trackLength.setText(makeTextWithTitle(context, R.string.label_track_length, MusicUtil.getReadableDurationString(audioHeader.getTrackLength() * 1000))); bitRate.setText(makeTextWithTitle(context, R.string.label_bit_rate, audioHeader.getBitRate() + " kb/s")); samplingRate.setText(makeTextWithTitle(context, R.string.label_sampling_rate, audioHeader.getSampleRate() + " Hz")); } } } catch (@NonNull CannotReadException | IOException | TagException | ReadOnlyFileException | InvalidAudioFrameException e) { Log.e(TAG, "error while reading the song file", e); } return dialog; }
Example #8
Source File: SongDetailDialog.java From VinylMusicPlayer with GNU General Public License v3.0 | 4 votes |
@NonNull @Override public Dialog onCreateDialog(Bundle savedInstanceState) { final Activity context = getActivity(); final Song song = getArguments().getParcelable("song"); MaterialDialog dialog = new MaterialDialog.Builder(context) .customView(R.layout.dialog_file_details, true) .title(context.getResources().getString(R.string.label_details)) .positiveText(android.R.string.ok) .build(); View dialogView = dialog.getCustomView(); final TextView fileName = dialogView.findViewById(R.id.file_name); final TextView filePath = dialogView.findViewById(R.id.file_path); final TextView fileSize = dialogView.findViewById(R.id.file_size); final TextView fileFormat = dialogView.findViewById(R.id.file_format); final TextView trackLength = dialogView.findViewById(R.id.track_length); final TextView bitRate = dialogView.findViewById(R.id.bitrate); final TextView samplingRate = dialogView.findViewById(R.id.sampling_rate); final TextView replayGain = dialogView.findViewById(R.id.replay_gain); fileName.setText(makeTextWithTitle(context, R.string.label_file_name, "-")); filePath.setText(makeTextWithTitle(context, R.string.label_file_path, "-")); fileSize.setText(makeTextWithTitle(context, R.string.label_file_size, "-")); fileFormat.setText(makeTextWithTitle(context, R.string.label_file_format, "-")); trackLength.setText(makeTextWithTitle(context, R.string.label_track_length, "-")); bitRate.setText(makeTextWithTitle(context, R.string.label_bit_rate, "-")); samplingRate.setText(makeTextWithTitle(context, R.string.label_sampling_rate, "-")); replayGain.setText(makeTextWithTitle(context, R.string.label_replay_gain, "-")); if (song != null) { final File songFile = new File(song.data); if (songFile.exists()) { fileName.setText(makeTextWithTitle(context, R.string.label_file_name, songFile.getName())); filePath.setText(makeTextWithTitle(context, R.string.label_file_path, songFile.getAbsolutePath())); fileSize.setText(makeTextWithTitle(context, R.string.label_file_size, getFileSizeString(songFile.length()))); try { AudioFile audioFile = AudioFileIO.read(songFile); AudioHeader audioHeader = audioFile.getAudioHeader(); fileFormat.setText(makeTextWithTitle(context, R.string.label_file_format, audioHeader.getFormat())); trackLength.setText(makeTextWithTitle(context, R.string.label_track_length, MusicUtil.getReadableDurationString(audioHeader.getTrackLength() * 1000))); bitRate.setText(makeTextWithTitle(context, R.string.label_bit_rate, audioHeader.getBitRate() + " kb/s")); samplingRate.setText(makeTextWithTitle(context, R.string.label_sampling_rate, audioHeader.getSampleRate() + " Hz")); float rgTrack = song.getReplayGainTrack(); float rgAlbum = song.getReplayGainAlbum(); String replayGainValues = ""; if (rgTrack != 0.0) { replayGainValues += String.format("%s: %.2f dB ", context.getString(R.string.song), rgTrack); } if (rgAlbum != 0.0) { replayGainValues += String.format("%s: %.2f dB ", context.getString(R.string.album), rgAlbum); } if (replayGainValues.equals("")) { replayGainValues = context.getString(R.string.none); } replayGain.setText(makeTextWithTitle(context, R.string.label_replay_gain, replayGainValues)); } catch (@NonNull CannotReadException | IOException | TagException | ReadOnlyFileException | InvalidAudioFrameException e) { Log.e(TAG, "error while reading the song file", e); // fallback trackLength.setText(makeTextWithTitle(context, R.string.label_track_length, MusicUtil.getReadableDurationString(song.duration))); } } else { // fallback fileName.setText(makeTextWithTitle(context, R.string.label_file_name, song.title)); trackLength.setText(makeTextWithTitle(context, R.string.label_track_length, MusicUtil.getReadableDurationString(song.duration))); } } return dialog; }
Example #9
Source File: SongDetailDialog.java From Phonograph with GNU General Public License v3.0 | 4 votes |
@NonNull @Override public Dialog onCreateDialog(Bundle savedInstanceState) { final Activity context = getActivity(); final Song song = getArguments().getParcelable("song"); MaterialDialog dialog = new MaterialDialog.Builder(context) .customView(R.layout.dialog_file_details, true) .title(context.getResources().getString(R.string.label_details)) .positiveText(android.R.string.ok) .build(); View dialogView = dialog.getCustomView(); final TextView fileName = dialogView.findViewById(R.id.file_name); final TextView filePath = dialogView.findViewById(R.id.file_path); final TextView fileSize = dialogView.findViewById(R.id.file_size); final TextView fileFormat = dialogView.findViewById(R.id.file_format); final TextView trackLength = dialogView.findViewById(R.id.track_length); final TextView bitRate = dialogView.findViewById(R.id.bitrate); final TextView samplingRate = dialogView.findViewById(R.id.sampling_rate); fileName.setText(makeTextWithTitle(context, R.string.label_file_name, "-")); filePath.setText(makeTextWithTitle(context, R.string.label_file_path, "-")); fileSize.setText(makeTextWithTitle(context, R.string.label_file_size, "-")); fileFormat.setText(makeTextWithTitle(context, R.string.label_file_format, "-")); trackLength.setText(makeTextWithTitle(context, R.string.label_track_length, "-")); bitRate.setText(makeTextWithTitle(context, R.string.label_bit_rate, "-")); samplingRate.setText(makeTextWithTitle(context, R.string.label_sampling_rate, "-")); if (song != null) { final File songFile = new File(song.data); if (songFile.exists()) { fileName.setText(makeTextWithTitle(context, R.string.label_file_name, songFile.getName())); filePath.setText(makeTextWithTitle(context, R.string.label_file_path, songFile.getAbsolutePath())); fileSize.setText(makeTextWithTitle(context, R.string.label_file_size, getFileSizeString(songFile.length()))); try { AudioFile audioFile = AudioFileIO.read(songFile); AudioHeader audioHeader = audioFile.getAudioHeader(); fileFormat.setText(makeTextWithTitle(context, R.string.label_file_format, audioHeader.getFormat())); trackLength.setText(makeTextWithTitle(context, R.string.label_track_length, MusicUtil.getReadableDurationString(audioHeader.getTrackLength() * 1000))); bitRate.setText(makeTextWithTitle(context, R.string.label_bit_rate, audioHeader.getBitRate() + " kb/s")); samplingRate.setText(makeTextWithTitle(context, R.string.label_sampling_rate, audioHeader.getSampleRate() + " Hz")); } catch (@NonNull CannotReadException | IOException | TagException | ReadOnlyFileException | InvalidAudioFrameException e) { Log.e(TAG, "error while reading the song file", e); // fallback trackLength.setText(makeTextWithTitle(context, R.string.label_track_length, MusicUtil.getReadableDurationString(song.duration))); } } else { // fallback fileName.setText(makeTextWithTitle(context, R.string.label_file_name, song.title)); trackLength.setText(makeTextWithTitle(context, R.string.label_track_length, MusicUtil.getReadableDurationString(song.duration))); } } return dialog; }
Example #10
Source File: Library.java From MusicPlayer with MIT License | 4 votes |
private static int writeXML(File directory, Document doc, Element songs, int i) { File[] files = directory.listFiles(); for (File file : files) { if (file.isFile() && isSupportedFileType(file.getName())) { try { AudioFile audioFile = AudioFileIO.read(file); Tag tag = audioFile.getTag(); AudioHeader header = audioFile.getAudioHeader(); Element song = doc.createElement("song"); songs.appendChild(song); Element id = doc.createElement("id"); Element title = doc.createElement("title"); Element artist = doc.createElement("artist"); Element album = doc.createElement("album"); Element length = doc.createElement("length"); Element trackNumber = doc.createElement("trackNumber"); Element discNumber = doc.createElement("discNumber"); Element playCount = doc.createElement("playCount"); Element playDate = doc.createElement("playDate"); Element location = doc.createElement("location"); id.setTextContent(Integer.toString(i++)); title.setTextContent(tag.getFirst(FieldKey.TITLE)); String artistTitle = tag.getFirst(FieldKey.ALBUM_ARTIST); if (artistTitle == null || artistTitle.equals("") || artistTitle.equals("null")) { artistTitle = tag.getFirst(FieldKey.ARTIST); } artist.setTextContent( (artistTitle == null || artistTitle.equals("") || artistTitle.equals("null")) ? "" : artistTitle ); album.setTextContent(tag.getFirst(FieldKey.ALBUM)); length.setTextContent(Integer.toString(header.getTrackLength())); String track = tag.getFirst(FieldKey.TRACK); trackNumber.setTextContent( (track == null || track.equals("") || track.equals("null")) ? "0" : track ); String disc = tag.getFirst(FieldKey.DISC_NO); discNumber.setTextContent( (disc == null || disc.equals("") || disc.equals("null")) ? "0" : disc ); playCount.setTextContent("0"); playDate.setTextContent(LocalDateTime.now().toString()); location.setTextContent(Paths.get(file.getAbsolutePath()).toString()); song.appendChild(id); song.appendChild(title); song.appendChild(artist); song.appendChild(album); song.appendChild(length); song.appendChild(trackNumber); song.appendChild(discNumber); song.appendChild(playCount); song.appendChild(playDate); song.appendChild(location); task.updateProgress(i, Library.maxProgress); } catch (Exception ex) { ex.printStackTrace(); } } else if (file.isDirectory()) { i = writeXML(file, doc, songs, i); } } return i; }
Example #11
Source File: XMLEditor.java From MusicPlayer with MIT License | 4 votes |
private static void createNewSongObject() { // Searches the xml file to get the last id assigned. int lastIdAssigned = xmlLastIdAssignedFinder(); // Loops through each song file that needs to be added and creates a song object for each. // Each song object is added to an array list and returned so that they can be added to the xml file. for (File songFile : songFilesToAdd) { try { AudioFile audioFile = AudioFileIO.read(songFile); Tag tag = audioFile.getTag(); AudioHeader header = audioFile.getAudioHeader(); // Gets song properties. int id = ++lastIdAssigned; String title = tag.getFirst(FieldKey.TITLE); // Gets the artist, empty string assigned if song has no artist. String artistTitle = tag.getFirst(FieldKey.ALBUM_ARTIST); if (artistTitle == null || artistTitle.equals("") || artistTitle.equals("null")) { artistTitle = tag.getFirst(FieldKey.ARTIST); } String artist = (artistTitle == null || artistTitle.equals("") || artistTitle.equals("null")) ? "" : artistTitle; String album = tag.getFirst(FieldKey.ALBUM); // Gets the track length (as an int), converts to long and saves it as a duration object. Duration length = Duration.ofSeconds((long) header.getTrackLength()); // Gets the track number and converts to an int. Assigns 0 if a track number is null. String track = tag.getFirst(FieldKey.TRACK); int trackNumber = Integer.parseInt((track == null || track.equals("") || track.equals("null")) ? "0" : track); // Gets disc number and converts to int. Assigns 0 if the disc number is null. String disc = tag.getFirst(FieldKey.DISC_NO); int discNumber = Integer.parseInt((disc == null || disc.equals("") || disc.equals("null")) ? "0" : disc); int playCount = 0; LocalDateTime playDate = LocalDateTime.now(); String location = Paths.get(songFile.getAbsolutePath()).toString(); // Creates a new song object for the added song and adds it to the newSongs array list. Song newSong = new Song(id, title, artist, album, length, trackNumber, discNumber, playCount, playDate, location); // Adds the new song to the songsToAdd array list. songsToAdd.add(newSong); } catch (Exception ex) { ex.printStackTrace(); } } // Updates the lastIdAssigned in MusicPlayer to account for the new songs. MusicPlayer.setLastIdAssigned(lastIdAssigned); }