Issue
I'm new to JavaFX and I'm currently developing a game.
I've managed to set background music in the app using the MediaPlayer
class.
Now what I need is to create a method that makes the song fade out (instead of stopping instantly).
I've been stuck in reading about transitions but don't really understand how to use them.
Thanks!!
Solution
The Timeline class can change any JavaFX property. By default, the property’s value is interpolated toward the target value, which (I believe) will provide the fade you desire:
Timeline timeline = new Timeline(
new KeyFrame(Duration.seconds(10),
new KeyValue(mediaPlayer.volumeProperty(), 0)));
timeline.play();
The KeyValue specifies the property you want to change (volume), and the target value (zero, that is, mute) for that property.
The KeyFrame specifies the time at which the KeyValue’s value should be in effect.
Answered By - VGR