[英]Can only play one sound clip at a time
我為游戲編寫了一個自定義的聲音系統,但是如果要求在彼此之間幾毫秒內播放兩種聲音,則只會播放一個聲音片段。
我嘗試在這樣的新線程上運行播放,但沒有成功。 沒有異常被拋出,它不會同時播放兩種聲音。
Thread one = new Thread() {
public void run() {
try {
CustomSound.playSound(id, loop, dist);
} catch (Exception e) {
e.printStackTrace();
}
}
};
這是聲音播放器類
public class CustomSound {
/*
* Directory of your sound files
* format is WAV
*/
private static final String DIRECTORY = sign.signlink.findcachedir()+"audio/effects/";
/*
* Current volume state
* 36 chosen for default 50% volume state
*/
public static float settingModifier = 70f;
/*
* Current volume state
*/
public static boolean isMuted;
/*
* Clips
*/
private static Clip[] clipIndex = null;
/*
* Get number of files in directory
*/
private static final int getDirectoryLength() {
return new File(DIRECTORY).list().length;
}
/**
* Loads the sound clips into memory
* during startup to prevent lag if loading
* them during runtime.
**/
public static void preloadSounds() {
clipIndex = new Clip[getDirectoryLength()];
int counter = 0;
for (int i = 0; i < clipIndex.length; i++) {
try {
File f = new File(DIRECTORY+"sound "+i+".wav");
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(f);
clipIndex[i] = AudioSystem.getClip();
clipIndex[i].open(audioInputStream);
counter++;
} catch (MalformedURLException e) {
System.out.println("Sound effect not found: "+i);
e.printStackTrace();
return;
} catch (UnsupportedAudioFileException e) {
System.out.println("Unsupported format for sound: "+i);
return;
} catch (LineUnavailableException e) {
e.printStackTrace();
return;
} catch (Exception e) {
e.printStackTrace();
return;
}
}
System.out.println("Succesfully loaded: "+counter+" custom sound clips.");
}
/**
* Plays a sound
* @param soundID - The ID of the sound
* @param loop - How many times to loop this sound
* @param distanceFromSource - The distance from the source in tiles
*/
public static void playSound(final int soundID, int loop, int distanceFromSource) {
try {
if (!isMuted) {
clipIndex[soundID].setFramePosition(0);
applyVolumeSetting(clipIndex[soundID], getDistanceModifier(distanceFromSource)*settingModifier);
if (loop == 1 || loop == 0) {
clipIndex[soundID].start();
} else {
clipIndex[soundID].loop(loop);
}
/* shows how to close line when clip is finished playing
clipIndex[soundID].addLineListener(new LineListener() {
public void update(LineEvent myLineEvent) {
if (myLineEvent.getType() == LineEvent.Type.STOP)
clipIndex[soundID].close();
}
});
*/
}
} catch (Exception e) {
System.out.println("Error please report: ");
e.printStackTrace();
}
}
/**
* Applies volume setting to the clip
* @param line - the Clip to adjust volume setting for
* @param volume - the volume percentage (0-100)
* @return - the volume with applied setting
*/
public static float applyVolumeSetting(Clip line, double volume) {
//System.out.println("Modifying volume to "+volume);
if (volume > 100.0) volume = 100.0;
if (volume >= 0.0) {
FloatControl ctrl = null;
try {
ctrl = (FloatControl)(line.getControl(FloatControl.Type.MASTER_GAIN));
} catch (IllegalArgumentException iax1) {
try {
ctrl = (FloatControl)(line.getControl(FloatControl.Type.VOLUME));
} catch (IllegalArgumentException iax2) {
System.out.println("Controls.setVolume() not supported.");
return -1;
}
}
float minimum = ctrl.getMinimum();
float maximum = ctrl.getMaximum();
float newValue = (float)(minimum + volume * (maximum - minimum) / 100.0F);
//System.out.println("System min: " + minimum);
//System.out.println("System max: " + maximum);
if (newValue <= ctrl.getMinimum())
newValue = ctrl.getMinimum();
if (newValue >= ctrl.getMaximum())
newValue = ctrl.getMaximum();
ctrl.setValue(newValue);
//System.out.println("Setting modifier = " + volume);
//System.out.println("New value = " + newValue);
return newValue;
}
return -1;
}
/**
* Calculates tile distance modifier
* @param tileDistance - distance in tiles from source
* @return - the distance modifier
*/
public static float getDistanceModifier(int tileDistance) {
if (tileDistance <= 0) {
tileDistance = 0;
}
if (tileDistance >= 10) {
tileDistance = 10;
}
float distanceModifier = 0;
if (tileDistance == 10)
distanceModifier = 0.40f;
if (tileDistance == 9)
distanceModifier = 0.55f;
if (tileDistance == 8)
distanceModifier = 0.60f;
if (tileDistance == 7)
distanceModifier = 0.65f;
if (tileDistance == 6)
distanceModifier = 0.70f;
if (tileDistance == 5)
distanceModifier = 0.75f;
if (tileDistance == 4)
distanceModifier = 0.80f;
if (tileDistance == 3)
distanceModifier = 0.85f;
if (tileDistance == 2)
distanceModifier = 0.90f;
if (tileDistance == 1)
distanceModifier = 0.95f;
if (tileDistance == 0)
distanceModifier = 1.00f;
return distanceModifier;
}
}
當我在Windows計算機上測試您的代碼時,我很快就可以連續播放兩種不同的聲音了:
public static void main(String[] args) throws Exception {
CustomSound.preloadSounds();
CustomSound.playSound(0, 0, 0);
CustomSound.playSound(1, 0, 0);
Thread.sleep(5000);
}
但是請注意, DataLine#start()
是異步調用。 那可能與您的問題有關。
另外,根據DataLine#start()
的文檔,
如果在已經運行的行上調用,則此方法不執行任何操作。
如果這是您的問題,並且您想同時播放兩次相同的聲音,則一種可能的解決方案是讓另一個Clip
實例播放相同的聲音並開始播放。
但是,我不是Java的聲音API的專家,所以可能會有更有效的方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.