【问题标题】:JavaFX - Music On/Off Toggle Button (Not Working)JavaFX - 音乐开/关切换按钮(不工作)
【发布时间】:2017-07-23 22:27:36
【问题描述】:

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

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

我认为 if 语句太简单了。我搜索了 JavaDocs 和各种在线文章,但找不到任何确定的东西。我读过一些关于听众的文章,但对于开/关开关来说,它们似乎过于复杂。

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

感谢您的帮助。

-巴格

/* 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);
}

}

【问题讨论】:

    标签: java user-interface button javafx


    【解决方案1】:

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

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

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

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

    【讨论】:

    • 非常简单的解决方案...非常感谢。这一切只需要在同一个处理程序块中......我现在明白了。再次感谢您!
    • 随时。如果这解决了您的问题,请选择它作为答案。
    猜你喜欢
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-18
    相关资源
    最近更新 更多