org.tritonus.share.sampled.file.TAudioFileFormat Java Examples

The following examples show how to use org.tritonus.share.sampled.file.TAudioFileFormat. 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: MusicController.java    From opsu-dance with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Returns the duration of the current track, in milliseconds.
 * Currently only works for MP3s.
 * @return the duration, or -1 if no track exists, else the {@code endTime}
 *         field of the beatmap loaded
 * @author Tom Brito (http://stackoverflow.com/a/3056161)
 */
public static int getDuration() {
	if (!trackExists() || lastBeatmap == null)
		return -1;

	if (duration == 0) {
		// TAudioFileFormat method only works for MP3s
		if (lastBeatmap.audioFilename.getName().endsWith(".mp3")) {
			try {
				AudioFileFormat fileFormat = AudioSystem.getAudioFileFormat(lastBeatmap.audioFilename);
				if (fileFormat instanceof TAudioFileFormat) {
					Map<?, ?> properties = ((TAudioFileFormat) fileFormat).properties();
					Long microseconds = (Long) properties.get("duration");
					duration = (int) (microseconds / 1000);
					return duration;
				}
			} catch (UnsupportedAudioFileException | IOException e) {}
		}

		// fallback: use beatmap end time (often not the track duration)
		duration = lastBeatmap.endTime;
	}
	return duration;
}
 
Example #2
Source File: MusicController.java    From opsu with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Returns the duration of the current track, in milliseconds.
 * Currently only works for MP3s.
 * @return the duration, or -1 if no track exists, else the {@code endTime}
 *         field of the beatmap loaded
 * @author Tom Brito (http://stackoverflow.com/a/3056161)
 */
public static int getDuration() {
	if (!trackExists() || lastBeatmap == null)
		return -1;

	if (duration == 0) {
		// TAudioFileFormat method only works for MP3s
		if (lastBeatmap.audioFilename.getName().endsWith(".mp3")) {
			try {
				AudioFileFormat fileFormat = AudioSystem.getAudioFileFormat(lastBeatmap.audioFilename);
				if (fileFormat instanceof TAudioFileFormat) {
					Map<?, ?> properties = ((TAudioFileFormat) fileFormat).properties();
					Long microseconds = (Long) properties.get("duration");
					duration = (int) (microseconds / 1000);
					return duration;
				}
			} catch (UnsupportedAudioFileException | IOException e) {}
		}

		// fallback: use beatmap end time (often not the track duration)
		duration = lastBeatmap.endTime;
	}
	return duration;
}