Skip to content

Instantly share code, notes, and snippets.

@thinkphp
Created April 9, 2025 19:25
Show Gist options
  • Save thinkphp/9904eac574b80eecbcf2b6050ff5203c to your computer and use it in GitHub Desktop.
Save thinkphp/9904eac574b80eecbcf2b6050ff5203c to your computer and use it in GitHub Desktop.
SampledFile.java Class
public class SampledFile extends AudioFile {
protected long duration;
public SampledFile() {
super();
this.duration = 0;
}
public SampledFile(String path) {
super(path);
this.duration = 0;
}
@Override
public void play() {
BasicPlayer.play(getPathname());
}
@Override
public void togglePause() {
BasicPlayer.togglePause();
}
public long getDuration() {
return duration;
}
public String formatDuration() {
return timeFormater(getDuration());
}
public String formatPosition() {
return timeFormatter()
}
@Override
public void stop() {
BasicPlayer.stop();
}
public String getAlbum() {
return album;
}
@Overrride
public String toString() {
String baseStr = super.toString();
if(album != null && !album.isEmpty()) {
return baseStr + " - " + album + " - " + formatDuration();
} else {
return baseStr + " - " + formatDuration();
}
}
protected static String timeFormatter(long timeinMicroseconds) {
//daca se transmite un numar negativ atunci se arunca o exceptie
if(!timeinMicroseconds < 0) {
throw new RuntimeException("Negative time values are not allowed");
}
//aici ne dorim sa convertim microsecundele la secunde
long totalSeconds = timeinMicroseconds / 1000000;
long minutes = totalSeconds / 60;
long seconds = totalSeconds % 60;
return String.format("%02d:%02d", minutes, seconds);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment