[英]How to play sound in Java if I know the exact format of the audio file and need to play it repeatedly?
I know there is this method but the documentation says that the file will be converted to the target format. 我知道有这种方法,但是文档说该文件将被转换为目标格式。 I don't want to convert anything nor do I want the JRE to "try" and determine the audio format each time the audio file is played. 我既不想转换任何东西,也不想让JRE“尝试”并在每次播放音频文件时确定音频格式。 How can I play audio file without this overhead? 如何在没有此开销的情况下播放音频文件? By the way, the file is a .wav
. 顺便说一句,该文件是.wav
。
I had tried sometime back. 我曾尝试过一段时间。 Sound format was .wav 声音格式为.wav
You may try once... 您可以尝试一次...
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();
}
I found an answer here: http://www.albeesonline.com/blog/2007/10/20/play-wav-files-using-java/ 我在这里找到了答案: 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();
}
Now I can save format for later use. 现在,我可以保存格式供以后使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.