So, I’m currently using a system that implements JavaFX into my game. The problem is: I’m wanting to repeatedly play the same sound effect multiple times. I’m making a Brick Breaker game, and I’m replaying the constant sound effect whenever a brick is hit. And, if you’ve ever played Brick Breaker, the ball can become stuck at the top and hit a ton of bricks sequentially, thus causing a ton of sound effects. Well, when I try to implement this with one MediaPlayer OR AudioClip JavaFX object, it only plays it once, and unless a slight amount of time goes by between bricks, it doesn’t play it again. My only work around is by, every time a brick is hit by the ball, create a NEW Audio object, then immediately play it. This WORKS, though, it is incredibly inefficient and slows my game down tremendously, and I have a powerful PC. Can anyone help me with this endeavor?
package com.joshuacrotts.pt.main;
import java.util.ArrayList;
import com.joshuacrotts.standards.StandardAudio;
public class SongBox {
public static ArrayList<StandardAudio> songs;
public static ArrayList<StandardAudio> sfx;
public SongBox(){
SongBox.songs = new ArrayList<StandardAudio>();
SongBox.songs.add(new StandardAudio("Resources/Audio/Music/menu.wav",false)); //Menu music
SongBox.songs.add(new StandardAudio("Resources/Audio/Music/level1.wav",false));//Level 1 music
SongBox.songs.add(new StandardAudio("Resources/Audio/Music/level2.wav",false));//Level 2 music
SongBox.sfx = new ArrayList<StandardAudio>();
SongBox.sfx.add(new StandardAudio("Resources/Audio/SFX/menuselect.wav",true));//Clicking on a button
SongBox.sfx.add(new StandardAudio("Resources/Audio/SFX/explode_1.wav",true));//Breaking a brick
SongBox.sfx.add(new StandardAudio("Resources/Audio/SFX/whoosh.wav",true));//Getting an item
// resetVolumes();
}
public void resetVolumes(){
for(int i = 0; i<songs.size(); i++){
songs.get(i).resetVolume();
}
for(int i = 0; i<sfx.size(); i++){
sfx.get(i).resetVolume();
}
}
/**
* This method will add a sound to the arraylists.
* @param audio
* @param sfx
*
* This is mainly so I can have more than one sound effect playing at once.
* THIS IS THE METHOD IM REFERENCING IN THE FORUM POST ****************
*/
public static void addSound(String audio, boolean sfx){
if(!sfx){
SongBox.songs.add(new StandardAudio(audio,true));
SongBox.songs.get(SongBox.songs.size()-1).play();
}else{
SongBox.sfx.add(new StandardAudio(audio,true));
SongBox.sfx.get(SongBox.sfx.size()-1).play();
}
}
public static void clearSFX(){
for(int i = 1; i<SongBox.sfx.size(); i++){
SongBox.sfx.remove(i);
i--;
}
}
}
//Below is my Audio (StandardAudio) class:
package com.joshuacrotts.standards;
import java.io.File;
import javafx.scene.media.AudioClip;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
/**
*
* Takes an audio source in and plays it.
*
*/
public class StandardAudio{
private String fileName;
//private Media media;
private AudioClip sound;
private boolean sfx;
public StandardAudio(String fileName, boolean sfx)
{
this.sfx = sfx;
new javafx.embed.swing.JFXPanel();
try{
this.sound = new AudioClip(new File(fileName).toURI().toString());
//this.sound = new MediaPlayer(this.media);
}catch(Exception e)
{
e.printStackTrace();
}
this.sound.setVolume(1);
}
public void play()
{
if(this.sound.isPlaying())
return;
else
this.sound.play();
if(!sfx)
this.loop();
}
public void adjustVolume(double val) {
this.sound.setVolume(this.sound.getVolume() + val);
}
public double getVolume(){
return this.sound.getVolume();
}
public void fadeToBlack() {
double val = -0.05D;
this.adjustVolume(val);
if(this.getVolume() <= 0) {
this.sound.stop();
this.sound.setVolume(1);
}
}
public void stop()
{
this.sound.stop();
//audioClip.close();
}
public String getFileName()
{
return fileName;
}
/**
* @param x determines if the loop is infinite. If it's 1, it is. Else, it's not.
*/
public void loop(){
this.sound.setCycleCount(MediaPlayer.INDEFINITE);
}
public void resetVolume(){
this.sound.setVolume(1);
}
}