简体   繁体   中英

JavaFX: Play a sound when ever something is clicked

I currently have my EventHandler here:

theScene.setOnMouseClicked(
            new EventHandler<MouseEvent>() {
                public void handle(MouseEvent e) {
                    if (targetData.containsPoint(e.getX(), e.getY())) {
                        double x = 50 + 400 * Math.random();
                        double y = 50 + 400 * Math.random();
                        targetData.setCenter(x, y);
                        mediaPlayer.play(); //play hitsound
                        points.value++; //add points
                    } else {
                        points.value = 0; //miss
                    }
                }
            });

I also have my audio setup like so:

    String hitNormal = ("hit.mp3");
    Media sound = new Media(new File(hitNormal).toURI().toString());
    MediaPlayer mediaPlayer = new MediaPlayer(sound);

When I run my program, only the first click on the circle plays the hitsound, but all the following circles do not play the sound. I think what I have above should work since every time I click additional circles it always adds score. Since I have my mediaPlayer.play(); in the exact same if statement as the line that adds score, I expected the sound to play as well. How do I get my hitsound to play every time I click the circle?

EDIT: I added mediaPlayer.stop(); under points.value++ and it seems to occasionally play the hitsound but not always.

In the question comments you mention the audio is only a second long. If that's the case using an AudioClip might be better suited for your needs than Media / MediaPlayer .

An AudioClip can be played more than once in parallel (allowing multiple quick hits to play the sound at once) unlike a MediaPlayer . It also can be played as many times as you want without much hassle.

If you stick with a MediaPlayer James_D and TomN seem to be on the right track. From some little testing it seems a MediaPlayer , after reaching the end the media, doesn't simply restart when you call play() . The reason for this is because the Status of the MediaPlayer doesn't change from PLAYING to STOPPED when the media is finished. And since play() does nothing when the Status is PLAYING it won't restart.

To get around this you have to first set the time to 0 by using seek(Duration.ZERO) and then call play() . In fact, after the first time, calling play() doesn't seem necessary if you haven't called pause() or stop() anywhere before.

However, since your audio is only a second long I still recommend AudioClip . The class is designed for exactly what you're doing from what I can tell.

You properly need to check the status of media player, this method may helps mediaPlayer.getStatus() .

when it's playing, another play() method seem to do nothing because it change status to playing but it already does. see MediaPlayer and MediaPlayer.Status


try use below method in your code.

mediaPlayer.stop() it will stop the media and reset current time to zero.

add this method when media is end. you can try as below code

mediaPlayer.setOnEndOfMedia(new Runnable() 
{
    public void run() 
    {
        mediaPlayer.stop();
    }
});

or use mediaPlayer.seek() seek current time.

when previous media isn't yet to reach end, you need to pause it and seek current time to zero before next play() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM