Skip to content
This repository has been archived by the owner on Oct 24, 2024. It is now read-only.

Commit

Permalink
Fixing some conditions in SoundManager.java
Browse files Browse the repository at this point in the history
  • Loading branch information
CiroZDP committed Apr 25, 2024
1 parent a7e10b0 commit 9b81fca
Showing 1 changed file with 79 additions and 80 deletions.
159 changes: 79 additions & 80 deletions src/main/java/net/op/sound/SoundManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,85 +13,84 @@

public class SoundManager {

private static final Clip player;
public static final Logger logger = Logger.getLogger(SoundManager.class.getName());
public static boolean ENABLED = false;

static {
// Create clip instance
Clip clip = null;
try {
clip = AudioSystem.getClip();
} catch (Exception ex) {
InternalLogger.out.println(SoundManager.class.getName() + " ->");
ex.printStackTrace(InternalLogger.out);
InternalLogger.out.println();

clip = null;
ENABLED = false;
}

player = clip;
logger.info("Sound manager started!");
logger.info("[SoundAPI] Sound API status: %s".formatted(ENABLED ? "Active" : "Passive"));
}

private SoundManager() {
}

public static void update() {
if (!ENABLED || player == null) {
stopSounds();
return;
}

if (player.isActive()) {
return;
}

int r = (int) (System.currentTimeMillis() / 1000 % 10);
if (r == 0) {
Stream<Sound> sounds = Tracks.get("Menu Sounds").getSounds().filter(sound -> !sound.isSynthwave());
List<Sound> soundList = sounds.toList();
int index = new Random().nextInt(soundList.size());
playSound(soundList.get(index), false);
}

}

public static void playSound(Sound sound, boolean loop) {
if (player == null || sound == null) {
return;
}

try {
AudioInputStream audioStream = AudioSystem.getAudioInputStream(sound.inputStream());
player.open(audioStream);
player.loop(loop ? Clip.LOOP_CONTINUOUSLY : 0);
} catch (Exception ex) {
InternalLogger.out.println(SoundManager.class.getName() + " ->");
ex.printStackTrace(InternalLogger.out);
InternalLogger.out.println();
}
}

public static void stopSounds() {
try (player) {
player.loop(0);
player.stop();
}
}

public static void enable() {
ENABLED = true;
}

public static void disable() {
ENABLED = false;
}

public static boolean isSupported() {
return ENABLED;
}
private static final Clip player;
public static final Logger logger = Logger.getLogger(SoundManager.class.getName());
public static boolean ENABLED = false;

static {
// Create clip instance
Clip clip = null;
try {
clip = AudioSystem.getClip();
} catch (Exception ex) {
InternalLogger.out.println(SoundManager.class.getName() + " ->");
ex.printStackTrace(InternalLogger.out);
InternalLogger.out.println();

clip = null;
ENABLED = false;
}

player = clip;
logger.info("Sound manager started!");
logger.info("[SoundAPI] Sound API status: %s".formatted(ENABLED ? "Active" : "Passive"));
}

private SoundManager() {
}

public static void update() {
if (!ENABLED) {
stopSounds();
return;
}

if (player.isActive() || player == null)
return;

int r = (int) (System.currentTimeMillis() / 1000 % 10);
if (r == 0) {
Stream<Sound> sounds = Tracks.get("Menu Sounds").getSounds().filter(sound -> !sound.isSynthwave());
List<Sound> soundList = sounds.toList();
int index = new Random().nextInt(soundList.size());
playSound(soundList.get(index), false);
}

}

public static void playSound(Sound sound, boolean loop) {
if (player == null || sound == null) {
return;
}

try {
AudioInputStream audioStream = AudioSystem.getAudioInputStream(sound.inputStream());
player.open(audioStream);
player.loop(loop ? Clip.LOOP_CONTINUOUSLY : 0);
} catch (Exception ex) {
InternalLogger.out.println(SoundManager.class.getName() + " ->");
ex.printStackTrace(InternalLogger.out);
InternalLogger.out.println();
}
}

public static void stopSounds() {
try (player) {
player.loop(0);
player.stop();
}
}

public static void enable() {
ENABLED = true;
}

public static void disable() {
ENABLED = false;
}

public static boolean isSupported() {
return ENABLED;
}

}

0 comments on commit 9b81fca

Please sign in to comment.