JavaFX MediaPlayer playing background music loop w

2019-09-15 16:07发布

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 the mediaPlayer.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 if startTime 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 (using mediaPlayer.setOnEndOfMedia()). However, the transition was too slow and noticable.

1条回答
SAY GOODBYE
2楼-- · 2019-09-15 16:21

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.

查看更多
登录 后发表回答