Oyunumdaki seslerimi çalmak için JLayer kullanıyorum. Müziğim (sürekli çalma) ve ses efektleri (sporadik) için bağımsız ses seviyeleri ayarlamaya çalışıyorum. Şu anda kodum ana ses seviyesini değiştiriyor, beklendiği gibi, her ikisinin de hacmini değiştiriyor. İşte ne yapmak istediğimi göstermek için bazı örnek kodlar (SSCCE-gibi olmak için bir sürü şey kestim ve bazı "hatalar" var).Farklı ses düzeylerinde aynı anda java mp3'leri oynayın
Herhangi bir yardım büyük ölçüde takdir edilecektir.
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();
}
}
}
@gpasch cevabınız nereye gitti? Uygulamayı bitirdim ve harika çalışıyor. Bounty atanamadı :( – KisnardOnline