一次只能播放一个声音片段
Posted
技术标签:
【中文标题】一次只能播放一个声音片段【英文标题】:Can only play one sound clip at a time 【发布时间】:2014-03-02 20:53:20 【问题描述】:我为我的游戏编写了一个自定义声音系统,但如果要求在几毫秒内播放两个声音,则只会播放一个声音片段。
我尝试在这样的新线程上运行播放,但它不起作用。 没有抛出异常,只是不会播放两种声音。
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;
【问题讨论】:
你用过audiodj吗,它是一个声音播放监督器。 不,我不知道那是什么,我使用的是我自己发布的播放器 是否抛出异常?此外,您似乎已经将您对playSound
的呼叫包装在一个空的try-catch 中。这不是一件好事。
不,你在哪里看到一个空的try catch?
我认为@Radiodef 意味着您的新线程的catch 方法是空的(在代码的顶部块中),因此您无法知道CustomSound 线程中是否存在未捕获的异常。
【参考方案1】:
当我在我的 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 方面的专家,所以可能有更有效的方法。
【讨论】:
它不会每次都发生,可能大约有 10% 的机会。If invoked on a line that is already running, this method does nothing.
如何将其他声音作为单独的剪辑实例播放?
最简单的方法是从其声音文件中加载剪辑,就像您在preloadSounds()
中所做的那样,只要您检测到您要播放的剪辑已经是running。跨度>
以上是关于一次只能播放一个声音片段的主要内容,如果未能解决你的问题,请参考以下文章