[英]How to play sound in Java if I know the exact format of the audio file and need to play it repeatedly?
我知道有这种方法,但是文档说该文件将被转换为目标格式。 我既不想转换任何东西,也不想让JRE“尝试”并在每次播放音频文件时确定音频格式。 如何在没有此开销的情况下播放音频文件? 顺便说一句,该文件是.wav
。
我曾尝试过一段时间。 声音格式为.wav
您可以尝试一次...
public static synchronized void audioPlay(String fileLoc) {new Thread(new Runnable() {
public void run() {
try {
Clip clip = AudioSystem.getClip();
AudioInputStream ais= AudioSystem.getAudioInputStream(
Main.class.getResourceAsStream("the path of the wav file" + fileLoc));
clip.open(ais);
clip.start();
}
catch (Exception ex) {
ex.printStackTrace();
System.out.println(ex.getMessage);
}
}
}).start();
}
我在这里找到了答案: http : //www.albeesonline.com/blog/2007/10/20/play-wav-files-using-java/
try {
AudioInputStream stream =
AudioSystem.getAudioInputStream(new File("C:\\i386\\Blip.wav"));
AudioFormat format = stream.getFormat();
DataLine.Info info =
new DataLine.Info(Clip.class,format);
Clip clip = (Clip) AudioSystem.getLine(info);
clip.open(stream);
clip.start();
} catch (Exception e) {
e.printStackTrace();
}
现在,我可以保存格式供以后使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.