I am trying to create a game using JavaFX. I had been trying to insert some background musics to the game. The musics are of compatible .mp3 files. I am currently using MediaPlayer
to playback each Media
. Each media file consist of the actual loop and a small (optional) intro music to the loop. How can I achieve smooth audio playback using JavaFX.
Methods I have tried:
Using the
Media
as one file; I try to alter themediaPlayer.startTime
,mediaPlayer.stopTime
. It seems that the duration of the music is shortened when I enter the loop. However, the music restarts at the intro and ends at the wrong place. It was as ifstartTime
didn't offset the music at all.Using the
Media
as two separate files (intro and loop); I try to immediately playback the loop music as soon as the intro music is finished (usingmediaPlayer.setOnEndOfMedia()
). However, the transition was too slow and noticable.
After experience with other well-known game engines (e.g. Unity), I found that the files should be in
.wav
format as opposed to.mp3
format. This will allow the music to be looped seamlessly.