JavaFX: Play a sound when ever something is clicked

462
May 31, 2017, at 1:55 PM

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.

Answer 1

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

Rent Charter Buses Company
READ ALSO
How to scan hbase in a map reduce job

How to scan hbase in a map reduce job

I need to run hourly analysis of elements that are stored in hbase, which means for each element, I need to get the previous hour's data to analyzeThe only problem is it's taking me more than 1 hour to scan everything when using a for loop without mapreduce

350
Android - Error Converting Bytecode to Dex with min sdk version

Android - Error Converting Bytecode to Dex with min sdk version

I am trying to run my app and keep encountering the error:

516
java assistance for looping the main code

java assistance for looping the main code

Im trying to write a code and i used the do while loop in an atempt to loop my main code after the user types in y or yes (case insensitive) into an InputDialog

318
Java Logging with Decorator pattern

Java Logging with Decorator pattern

Logging error messages is a requirement that is needed by the console as well as GUI applicationsTherefore the actual error logging functionality can be placed inside a common utility class so that client objects do not have to repeat these details

463