Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
1.3 kB
2
Indexable
Never
package manager1;

import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.application.Platform;
import java.io.File;

public class SoundManager1 {

    public static int WAITING_TIME = 0;

    private boolean playSound = false;
    private String path;
    private MediaPlayer mediaPlayer;

    public SoundManager1(String path) {
    	this.path = path;
    	Media sound = new Media(new File(path).toURI().toString());
    	this.mediaPlayer = new MediaPlayer(sound);
    	mediaPlayer.play();
    	
    }

    public void playsound() {
        // Run the sound playing task on the JavaFX Application Thread
        Platform.runLater(() -> {
            if (playSound) {
                mediaPlayer.stop();  // Stop any currently playing media
                mediaPlayer.play();  // Play the new media
                playSound = false;
            }
        });
    }

    // Sets playSound to true and calls play method
    public void triggerPlay() {
        playSound = true;
        playsound();
    }
    
    // Stops the currently playing sound
    public void stop() {
        Platform.runLater(() -> {
            if (mediaPlayer != null) {
                mediaPlayer.stop();
            }
        });
    }
}

Leave a Comment