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

时间:2017-03-03 17:35:38

标签: java user-interface button javafx

下面是代码示例,其他功能遗漏了。以下代码仅包含媒体播放器。它用于我正在进行的项目的菜单屏幕上。我的问题是让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);
}

}

1 个答案:

答案 0 :(得分:2)

我认为你是在思考它。您应EventListener ToggleButton暂停和播放音乐。

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

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

您的代码无法正常工作的原因是默认情况下未选中ToggleButton,因此与EventListener相关联的唯一menuPlayer.pause();EventListener。所以当你点击它时,它只会暂停。我已将您的代码移到一个if-else,并使用了适当的 angular.module('starter', []) .controller('Ctrl', function($scope) { $scope.data = {}; //init variable $scope.click = function() { //default function, to be override if browser supports input type='file' $scope.data.alert = "Your browser doesn't support HTML5 input type='File'" } var fileSelect = document.createElement('input'); //input it's not displayed in html, I want to trigger it form other elements fileSelect.type = 'file'; if (fileSelect.disabled) { //check if browser support input type='file' and stop execution of controller return; } $scope.click = function() { //activate function to begin input file on click fileSelect.click(); } fileSelect.onchange = function() { //set callback to action after choosing file var f = fileSelect.files[0], r = new FileReader(); if(f.type === 'image/jpeg' || f.type === 'image/png') { console.log('The file type is ' + f.type); // do something here } else { console.log('The file type is ' + f.type); // do some other thing here } r.onloadend = function(e) { //callback after files finish loading $scope.data.b64 = e.target.result; $scope.$apply(); console.log($scope.data.b64.replace(/^data:image\/(png|jpg);base64,/, "")); //replace regex if you want to rip off the base 64 "header" //here you can send data over your server as desired } r.readAsDataURL(f); //once defined all callbacks, begin reading the file }; });