我正在结束JavaFX游戏,其中的功能之一是背景音乐以及添加到各种动作中的声音效果。在游戏的某个时刻,如果获胜,则会播放特殊的音效。
这是问题所在:
当获胜的音效播放完后,我希望背景音乐暂停并在音效完成后再次播放。我已经尝试过操纵线程,但是每次这样做,异常都会跳出来。
以下是您可能感兴趣的代码:
// That is the method in which upon winning (first "else" segment) you hear the sound effect
public void checkAction() {
if (checkNumberOfGuesses < OptionsController.numberOfAllowedTries) {
int guess = Integer.parseInt(guessNumberInputField.getText());
if (guess < theNumber) {
resultLabel.setText("Too low");
wrongGuessSoundEffect();
} else if (guess > theNumber) {
resultLabel.setText("Too high");
wrongGuessSoundEffect();
} else {
resultLabel.setText("Correct!\nYou win!");
correctGuessSoundEffect(); // <- invoking the method that runs sound effect
playAgainPane.setVisible(true);
checkNumberOfGuesses = 0;
}
} else {
resultLabel.setText("You loose...");
gameOverSoundEffect();
playAgainPane.setVisible(true);
checkNumberOfGuesses = 0;
}
checkNumberOfGuesses++;
}
public void correctGuessSoundEffect() {
String correctGuessFilePath = "C:/Users/Stanisław/IdeaProjects/GuessingApp_GUI/src/com/javafx/sounds/correctGuess2.mp3";
Media correctGuessSound = new Media(new File(correctGuessFilePath).toURI().toString());
correctGuessPlayer = new MediaPlayer(correctGuessSound);
correctGuessPlayer.play();
}
// THIS method is located in different class than the other two
public static void backgroundMusic(String backgroundMusicPath) throws InterruptedException {
Media backgroundMusic = new Media(Paths.get(backgroundMusicPath).toUri().toString());
backgroundMusicPlayer = new MediaPlayer(backgroundMusic);
backgroundMusicPlayer.setCycleCount(MediaPlayer.INDEFINITE);
backgroundMusicPlayer.play();
}
另一个可能的复杂性是 backgroundMusic()方法是通过Main类的 start(Stage primaryStage)方法运行的。我必须将它保存在那里,以便音乐在每个窗口中播放(我在游戏中总共有5个窗口)。
我刚刚开始编码,因此解决方案可能很简单。我将非常感谢我能获得的所有帮助。