Newer
Older
package sample;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.Media;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
public class SoundController{
private List<String> sounds = new ArrayList<>();
//Creating Singleton
private static final SoundController INSTANCE = new SoundController();
private SoundController(){
addSound("./src/sample/woosh1.mp3");
addSound("./src/sample/woosh2.mp3");
addSound("./src/sample/woosh3.mp3");
addSound("./src/sample/woosh4.mp3");
}
public static SoundController getInstance(){
return INSTANCE;
}
//End Singleton
public void addSound(String file){
this.sounds.add(file);
}
public void playSound(){
Media sound = new Media(new File(sounds.get(ThreadLocalRandom.current().nextInt(0, sounds.size()))).toURI().toString());
MediaPlayer mediaPlayer = new MediaPlayer(sound);
mediaPlayer.play();
}
}