exception of reflect.Invocationtargetexception

Asked

Viewed 56 times

0

hello, I’m trying to make an application that runs a song endlessly, but when the user clicks a button the music stops , that 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 the Timeline clock was supposed to stop

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

but it is exactly in "clock.stop()" that I catch this exception, ?

  • What is the type of the clip variable? Mediaplayer?

1 answer

0

I created an example from the available code and it worked. A few things that might have influenced:

  1. I used the javafx Mediaplayer to play the song. The question does not say which class is being used to play the song, by the variable name, seems to have been the class javax.sound.sampled.Clip.
  2. The question does not say how these methods are being called, in my case I did the implementation on the button onMouseClicked.
  3. I used the java8.
  4. I used a. mp3 file.

Follows code:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.