title: JavaFX - Music On/Off Toggle Button (Not Working)
body: <p>Below is the code sample, with other features left out. The code below encompasses the media player only. It's used on a menu screen for a project I'm working on. My issue is getting the musicButton (which is a toggle button- On/Off) to work properly. Using the following code, when I interact with the music toggle button, the playing music stops. When I click it again to resume playing, it does not resume. It stops once and stops altogether.</p>
  
  <p>You can see I've tried simply using the boolean values of the toggle button in two if statements... If it's off and pressed, pause the music. If its on and pressed, resume the music. The problem is, as stated earlier, pausing the music works but it cannot be resumed. I've tried some combinations with loops, but nothing worked either.</p>
  
  <p>I think if statements are too simple for this. I've scoured the JavaDocs and various online articles but I cannot find anything definitive. I've read a little about listeners, but they seem overly-complex for an on/off switch. </p>
  
  <p>My question:
  How do I get the musicButton to pause/play the music, whenver the user clicks it?</p>
  
  <p>Any help is appreciated, thanks.</p>
  
  <p>-Bagger</p>
  
  <pre><code>/* 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 -&gt; menuPlayer.pause());
      } 
  
      if (musicButton.isSelected() == true) {
  
          musicButton.setOnAction(e -&gt; 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);
  }
  
  }
  </code></pre>