gpt4 book ai didi

JavaFX - 音乐开/关切换按钮(不起作用)

转载 作者:行者123 更新时间:2023-12-02 02:59:24 25 4
gpt4 key购买 nike

下面是代码示例,省略了其他功能。下面的代码仅包含媒体播放器。它用在我正在开发的项目的菜单屏幕上。我的问题是让 musicButton (这是一个切换按钮 - 开/关)正常工作。使用以下代码,当我与音乐切换按钮交互时,播放音乐停止。当我再次单击它以恢复播放时,它不会恢复。它停止一次并完全停止。

您可以看到我尝试在两个 if 语句中简单地使用切换按钮的 boolean 值...如果它关闭并按下,则暂停音乐。如果其打开并按下,则恢复音乐。问题是,如前所述,暂停音乐作品但无法恢复。我尝试了一些与循环的组合,但也没有任何效果。

我认为 if 语句对此来说太简单了。我浏览了 JavaDocs 和各种在线文章,但找不到任何明确的内容。我读过一些关于监听器的内容,但它们对于开关来说似乎过于复杂。

我的问题:当用户单击它时,如何让 musicButton 暂停/播放音乐?

感谢任何帮助,谢谢。

-Bagger

/* A simple game, the mechanics not yet implemented.

This is simply working on the title screen. */


import java.io.File;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.ToggleButton;
import javafx.scene.layout.VBox;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.stage.Stage;

public class MenuFX extends Application {

@Override

public void start (Stage primaryStage) {

// Make the window a set size...
primaryStage.setResizable(false);

// Create media player
// Rather than inputting the entire absolute URI, which would confine the program
// to the creator's device, we create a new file, grab the URI on whatever machine
// the program is running on and convert it to a string... portability.
Media menuMusic = new Media(new File("music/menu.mp3").toURI().toString());
MediaPlayer menuPlayer = new MediaPlayer(menuMusic);

// Want to see the absolute URI? Uncomment the next line
//System.out.println(new File("music/menu.mp3").toURI().toString());

// Adjust the cycles and volume then start playing menu music
// Lazy, but it will suffice
menuPlayer.setCycleCount(999999999);
menuPlayer.setVolume(0.1);
menuPlayer.setAutoPlay(true);




/*
Need assistance here
*/


// Create music toggle button
ToggleButton musicButton = new ToggleButton("Music On/Off");

if (musicButton.isSelected() == false) {

musicButton.setOnAction(e -> menuPlayer.pause());
}

if (musicButton.isSelected() == true) {

musicButton.setOnAction(e -> menuPlayer.play());
}




// Add all nodes to the vbox pane and center it all
// Must be in order from top to bottom
menuVBox.getChildren().add(musicButton);
menuVBox.setAlignment(Pos.CENTER);

// New scene, place pane in it
Scene scene = new Scene(menuVBox, 630, 730);

// Place scene in stage
primaryStage.setTitle("-tiles-");
primaryStage.setScene(scene);
primaryStage.show();
}

// Needed to run JavaFX w/o the use of the command line
public static void main(String[] args) {

launch(args);
}

}

最佳答案

我觉得你想多了。您的 ToggleButton 上应该有一个 EventListener 来暂停和播放音乐。

musicButton.setOnAction(event -> {
if (musicButton.isSelected()) {
menuPlayer.pause();
}else {
menuPlayer.play();
}
});

这应该会给你带来想要的效果。

您的代码无法正常工作的原因是默认情况下未选择 ToggleButton,因此与其关联的唯一 EventListenermenuPlayer。暂停();。因此,当您单击它时,它只会暂停。我已将您的代码移至一个 EventListener 中,并使用了适当的 if-else

关于JavaFX - 音乐开/关切换按钮(不起作用),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42585186/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com