3

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.stack.imgur.com/McyLD.jpg -- difference between debug and normal build

Kelevra
  • 95
  • 10
  • 1
    Assuming you are using MP3: Duration of VBR (variable bit rate) encoded songs is hard to calculate. Check whether your songs are CBR or VBR – Dio F Sep 02 '13 at 12:19
  • What library does the `Media` class come from? – Stewart Sep 02 '13 at 12:27
  • @Dio F Windows explorer says 128kbps, VLC says input varies around 126 to -129 kbps – Kelevra Sep 02 '13 at 12:30
  • 1
    Then it's probably VBR. I am not an expert for this. My only advice would be to check different libraries, like [SoX](http://sox.sourceforge.net/), which may give better results. – Dio F Sep 02 '13 at 12:47
  • If I run debugger it runs perfectly fine. – Kelevra Sep 02 '13 at 13:00

1 Answers1

0

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.

Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
Klaus Eckelt
  • 676
  • 4
  • 12