reflect.InvocationTargetException

0

Hello, I'm trying to make an application that runs a song infinitely, but when the user clicks a button the music stops, this is my code

public void tocar() { 
 relogio = new Timeline(new KeyFrame(Duration.ZERO, e -> {

      clip.play();
  }),
        new KeyFrame(Duration.seconds(10))
   );
   relogio.setCycleCount(Animation.INDEFINITE);
   relogio.play();
}

Here is the button where you should supposedly stop the clock timeline

  public void eventoMusica(ActionEvent evento){
    if (gettexto().equals("desligar")){
        clip.stop();
        relogio.stop();
        botaoEvento.setText("ligar");
    }
    else{
        tocar();
        botaoEvento.setText("desligar");
    }
}

However, it is exactly in "relogio.stop ()" that I capture this exception, how do I resolve this?

    
asked by anonymous 24.06.2017 / 18:21

1 answer

0

I created an example from the code provided and it worked. Some things that may have influenced:

  • I used javafx's MediaPlayer to play music. The question does not say which class is being used to play the song, by the name of the variable, it appears to have been the javax.sound.sampled.Clip class.
  • The question does not say how these methods are being called, in my case I did the implementation on the onMouseClicked button.
  • I used java8.
  • I used an .mp3 file.
  • Follow the code below:

    String path = "music.mp3";
    File file = new File(path);
    MediaPlayer mediaPlayer = new MediaPlayer(new Media(file.toURI().toString()));
    Timeline relogio = new Timeline(new KeyFrame(Duration.ZERO, e -> mediaPlayer.play()), 
            new KeyFrame(Duration.seconds(10)));
    
    relogio.setCycleCount(Animation.INDEFINITE);
    relogio.play();
    
    Button button = new Button("desligar");
    button.setOnMouseClicked(event -> {
        if (button.getText().equals("desligar")){
            mediaPlayer.stop();
            relogio.stop();
            button.setText("ligar");
        }
        else{
            mediaPlayer.play();
            relogio.play();
            button.setText("desligar");
        }
    });
    
    Pane pane = new Pane();
    pane.getChildren().add(button);
    
    Scene scene = new Scene(pane,400,400);
    primaryStage.setScene(scene);
    primaryStage.show();
    
        
    05.12.2017 / 16:44