1

How can I play an audio with the animation on the same time.

    private final AudioClip soundRed = new AudioClip(getClass().getResource("resources/soundRed.mp3").toExternalForm());

    private void computerButtonBlink() {
        buttonTransparent(true);
        SequentialTransition s = new SequentialTransition();
        s.setDelay(Duration.seconds(1.2));
        s.setCycleCount(1);
        s.setAutoReverse(false);

        for (int i = 0; i < gameLogic.getComputerArray().size(); i++) {
            switch (gameLogic.getComputerArray().get(i)) {
            case 0:
                FadeTransition redft = new FadeTransition(Duration.millis(400), bRed);
                redft.setAutoReverse(true);
                redft.setFromValue(1.0);
                redft.setToValue(0.1);
                redft.setCycleCount(2);
                soundRed.play(); // Sound will start before Animation... Too early...
                s.getChildren().add(redft);
                break;

            default:
                break;
            }

         s.play();
        }
    

And this will play the audio too late:

redft.setOnFinished(new EventHandler<ActionEvent>() {
            
            @Override
            public void handle(ActionEvent arg0) {
                soundRed.play();
            }
        });

Adding a delay on the audio is not what I'm looking for.

Detrua
  • 59
  • 6
  • There is a comment "Sound will start before Animation" -> how long before the animation does the sound start? The AudioClip is designed to play immediately. JavaFX will, by default process animation frames at sixty per second. I would guess that if the animation doesn't start in the current frame, it will start in the next one. I doubt such a short time lapse would be perceiptable to most people. – jewelsea May 12 '22 at 20:24
  • 1
    You have a sequential transition into which it seems that you may be adding multiple fade transitions, each of which has cycle counts. Given there are multiple transitions and cycles it is difficult to tell what you actually want synchronized. Perhaps you want to play the sound more than once per fade transition, for each cycle, or when each sequential fade transition starts (neither of which your current code is coded to do). Best to provide a [mcve] that can replicate the issue via copy and paste and just shows your issue without cycles and sequences and game logic. – jewelsea May 12 '22 at 20:29
  • Not sure if this is what you're looking for, nor if it will give you the timing you want, but you can listen to the [status property](https://openjfx.io/javadoc/18/javafx.graphics/javafx/animation/Animation.html#statusProperty) and call `play()` when the status changes to `RUNNING`. – Slaw May 12 '22 at 22:30

0 Answers0