2017-03-03 103 views
0

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

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

我认为如果陈述过于简单。我搜索了JavaDocs和各种在线文章,但是我找不到任何明确的。我已经读了一些关于听众的内容,但是他们似乎对开/关切换过于复杂。

我的问题: 如何让音乐按钮在用户点击时暂停/播放音乐?

任何帮助表示赞赏,谢谢。

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

} 

回答

0

我认为你是在想它。您应该在ToggleButton上有EventListener以暂停和播放音乐。

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

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

您的代码无法工作的原因是因为默认情况下未选择ToggleButton,所以与其关联的唯一EventListenermenuPlayer.pause();。所以当你点击它时,它只会暂停。我已将您的代码移入一个EventListener,并使用了适当的if-else

+0

非常简单的解决方案...非常感谢。这一切只需要在同一个处理程序块中...我现在看到了。再次感谢你! – Psychobagger

+0

随时。如果这解决了你的问题,请选择它作为答案。 –