因此,对于我的课堂项目,我正在做一个游戏。我想添加一个音量滑块来更改音乐音量。我很困在这里。请帮助我。
我正在关注本教程,并且能够在屏幕上显示它。但是,即使滑动滑杆,音量也不会改变。有人可以给我建议吗?谢谢。
private Scene createOptionScene() {
String path = "E:\\All Computer Science Materials\\Java 240 Project\\PrinceFX\\Music\\"
+ songs.getSong(1) + ".mp3";
Media media = new Media(new File(path).toURI().toString());
currentPlay = new AudioClip(media.getSource());
currentPlay.setCycleCount(MediaPlayer.INDEFINITE);
currentPlay.play();
// Volume Control
volumeSlider.setValue(currentPlay.getVolume() * 100);
volumeSlider.valueProperty().addListener(new InvalidationListener() {
@Override
public void invalidated(Observable observable) {
currentPlay.setVolume(volumeSlider.getValue() / 100);
}
});
HBox temp = new HBox();
temp.getChildren().addAll(volumeSlider);
temp.setTranslateX(850);
temp.setTranslateY(410);
volumeSlider.setMinWidth(300);
Image image = new Image(new File("E:\\All Computer Science Materials\\" +
"Java 240 Project\\PrinceFX\\image\\" + picture.getImage(2) + ".png").toURI().toString());
//Setting the image view
ImageView imageView = new ImageView(image);
//Setting the position of the image
imageView.setX(0);
imageView.setY(0);
//setting the fit height and width of the image view
imageView.setFitHeight(primaryScreenBounds.getHeight());
imageView.setFitWidth(primaryScreenBounds.getWidth());
//Setting the preserve ratio of the image view
imageView.setPreserveRatio(true);
Label labelOption = new Label();
Button goBack = new Button("Go Back to Main");
goBack.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
currentPlay.stop();
sceneStart = createStartScene();
stageOne.setScene(sceneStart);
}
});
// Button to the main page.
HBox layoutOp = new HBox(15);
layoutOp.getChildren().addAll(labelOption, goBack);
// Button coordinate.
layoutOp.setTranslateX(1400);
layoutOp.setTranslateY(750);
Group gOption = new Group(imageView, layoutOp, temp);
return new Scene(gOption, 200, 200);
}