I've written the following piece of code
Media hit = new Media(dir);
MediaPlayer player = new MediaPlayer(hit);
runMedia run = new runMedia(player);
Ui.changeGuiTitle("Now playing: "+getLastBitFromUrl(dirUnencoded.toString()));
Ui.updateInitialTime(getTime(hit.getDuration().toSeconds()));
getTime method:
System.out.println(time);
System.out.println(Math.round(time));
double seconds = Math.round(time);
double modulo = seconds % 60;
double hours = (seconds - modulo) / 3600;
double minutes = hours / 60;
String Time = "";
if (hours > 0.5)
{
Time = Math.round(hours) + " : " + Math.round(minutes) + " : "
+ Math.round(seconds);
}
else
{
Time = Math.round(minutes) + " : " + Math.round(seconds);
}
return Time;
It doesn't give an error, but my problem is some songs return 0
at .getDuration().toSeconds()
, and other songs return their value perfectly fine. What could cause this problem?
EDIT:
In debug mode a song contains 136280.816326
millis.. while System.out.println(time)
returns 0
https://i.sstatic.net/McyLD.jpg -- difference between debug and normal build
I experienced Problems with this too. After Creating the Media Object it needs some time to get fully initialized (my experience).
You can tryto call your getTime-Method in MediaPlayer's onReady or onPlaying methods.