Я использую JLayer для воспроизведения своих звуков в игре. Я пытаюсь установить независимые уровни громкости для моей музыки (постоянно играющей) и звуковых эффектов (спорадически). Прямо сейчас мой код изменяет уровень основной громкости, поэтому, как и ожидалось, он меняет громкость обоих. Вот пример кода, чтобы продемонстрировать, что я хочу сделать (я вырезал много вещей, чтобы попытаться быть похожим на SSCCE и понять, что есть некоторые «ошибки»).
Любая помощь будет очень признательна.
public static void playSoundOrMusic(String filename, String type) {
String soundFilename = "";
if (type.equals("SFX")){
soundFilename = "res/sounds/sfx/" + filename;
} else if (type.equals("MUSIC")){
soundFilename = "res/sounds/music/" + filename;
}
FileInputStream fis = null;
try {
fis = new FileInputStream(soundFilename);
} catch (FileNotFoundException e) {
LOGGER.error("Sound file missing", e);
}
BufferedInputStream bis = new BufferedInputStream(fis);
try {
if (type.equals("SFX")){
sfxPlayer = new Player(bis);
} else if (type.equals("MUSIC")){
musicPlayer = new Player(bis);
}
} catch (JavaLayerException e) {
LOGGER.error("Sound file issue", e);
} catch (ArrayIndexOutOfBoundsException e) {
LOGGER.error("Sound file issue", e);
}
if (type.equals("SFX")){
Info source = Port.Info.SPEAKER;
if (AudioSystem.isLineSupported(source)){
try {
Port outline = (Port) AudioSystem.getLine(source);
outline.open();
FloatControl volumeControl = (FloatControl) outline.getControl(FloatControl.Type.VOLUME);
volumeControl.setValue(OptionsJPanel.sfxVolume);
} catch (LineUnavailableException ex) {
LOGGER.error("Sound line issue", ex);
}
new Thread(new Runnable() {
@Override
public void run() {
try {
sfxPlayer.play();
} catch (Exception ex) {
LOGGER.error("Sound(sfx) playing issue", ex);
}
}
}).start();
}
}
if (type.equals("MUSIC")){
Info source = Port.Info.SPEAKER;
if (AudioSystem.isLineSupported(source)){
try {
Port outline = (Port) AudioSystem.getLine(source);
outline.open();
FloatControl volumeControl = (FloatControl) outline.getControl(FloatControl.Type.VOLUME);
volumeControl.setValue(OptionsJPanel.musicVolume);
} catch (LineUnavailableException ex) {
LOGGER.error("Sound line issue", ex);
}
new Thread(new Runnable() {
String threadFilename = filename;
@Override
public void run() {
try {
musicPlayer.play();
while(!musicPlayer.isComplete()){
Thread.currentThread();
Thread.sleep(1000);
}
playSoundOrMusic(threadFilename, type);
} catch (Exception ex) {
LOGGER.error("Sound(music) playing issue", ex);
}
}
}).start();
}
}
}