【发布时间】:2020-10-21 01:03:42
【问题描述】:
package me.Warrior;
import java.net.URL;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.FloatControl;
public class SoundPlayer {
static URL path=null;
static AudioInputStream inputStream;
public static Clip clip;
public static synchronized void Play(final String url,float Volume) {
new Thread(new Runnable() {
public void run() {
try {
clip = AudioSystem.getClip();
URL resource = getClass().getClassLoader().getResource(url);
path=resource;
inputStream = AudioSystem.getAudioInputStream(path);
clip.open(inputStream);
setVolume(Volume,clip);
clip.start();
} catch (Exception e) {
}
}
}).start();
}
public static void setVolume(float Volume,Clip clip) {
FloatControl gainControl =
(FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
float dB=(float)(Math.log(Volume)/Math.log(10)*20);
gainControl.setValue(dB);
}
}
**所以我在我的游戏中播放了很多音频,我认为有问题,而如果我在同一 60 秒内播放两个音频,则会弹出错误并冻结游戏半秒
-
我能否以某种方式确定剪辑何时开始播放,以便我可以停止下一个剪辑 (我想查看剪辑何时开始播放,而不是何时播放) (但这对我来说也很有趣)
-
有没有比这更好的播放音频的方法
【问题讨论】: