繁体   English   中英

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

[英]JavaFX - Music On/Off Toggle Button (Not Working)

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

您可以看到我已经在两个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.pause(); 因此,当您单击它时,它只会暂停。 我已经将您的代码移到一个EventListener ,并使用了适当的if-else

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM