Reputation: 23
I'm very new to Java, and after poring over the docs, I'm finding myself stuck.
I have a small program that uses the JavaFX MediaPlayer to play a wav file. My player object has a currentTimeProperty, and I'd like to display the output of this property in minutes:seconds during playback.
So I have this at the end of a function that takes in my wav file and initializes the player:
mediaPlayer = new MediaPlayer(wav);
mediaPlayer.currentTimeProperty().addListener(new TimeListener());
Then I have:
public class TimeListener implements ChangeListener {
public void changed(ObservableValue o, Object oldVal, Object newVal) {
//Update time display with MediaPlayer's current time:
currTimeLabel.setText(newVal.toString());
}
}
This works. During playback of the wav, my currTimeLabel
updates with the current time, in milliseconds. The thing is, I want this time in minutes:seconds.
The variable newVal
is listed as type "Duration", and the Duration class has methods like toMinutes()
and toSeconds()
, but I can't apply them to newVal
, and I don't understand why.
I can create a new Duration object:
Duration testDuration = Duration.millis(100000);
And then use the methods:
double secondsDuration = testDuration.toSeconds();
// 100.0
So if testDuration
and newVal
are both duration objects, why can't I apply the methods to newval
?
And in general, am I on the right track? I understand there will have to be some string formatting to get my output correct, but it seems to me that first I need to get the value from newVal in a non-string format.
Thanks in advance.
Upvotes: 1
Views: 740
Reputation: 10979
It's because your newVal is an Object and not a Duration. You could cast it and de-reference it like ((Duration)newVal).
The other better way is to make your method ChangeListener<Duration>
TimeListener = new ChangeListener<Duration>() {
@Override public void changed(ObservableValue<? extends Duration> o, Duration oldVal, Duration newVal) {
//now newVal is of Duration class
}
};
Upvotes: 1