JavaFX MediaPlayer播放带有小介绍音乐的背景音乐循环 [英] JavaFX MediaPlayer playing background music loop with small intro music
问题描述
我正在尝试使用JavaFX创建游戏.我一直试图在游戏中插入一些背景音乐.这些音乐具有兼容的.mp3文件.我当前正在使用MediaPlayer
播放每个Media
.每个媒体文件都包含实际的循环和循环的一小段(可选)简介音乐.如何使用JavaFX实现流畅的音频播放.
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.
我尝试过的方法:
-
将
Media
用作一个文件;我尝试更改mediaPlayer.startTime
,mediaPlayer.stopTime
.当我进入循环时,似乎音乐的持续时间缩短了.但是,音乐会在介绍处重新开始,并在错误的位置结束.好像startTime
根本没有抵消音乐.
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.
将Media
用作两个单独的文件(简介和循环);我尝试在介绍音乐结束后立即播放循环音乐(使用mediaPlayer.setOnEndOfMedia()
).但是,过渡太慢且引人注目.
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.
推荐答案
在使用其他知名游戏引擎(例如Unity)后,我发现文件应采用.wav
格式,而不是.mp3
格式.这将使音乐无缝地循环播放.
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.
这篇关于JavaFX MediaPlayer播放带有小介绍音乐的背景音乐循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!