如何在 Java 应用程序中播放音频 [英] How to play audio in Java Application
问题描述
我正在制作一个 Java 应用程序,我需要播放音频.我主要播放我的大炮射击(它是大炮射击游戏)和弹丸爆炸的小声音文件,尽管我计划播放循环背景音乐.我找到了两种不同的方法来实现这一点,但都不能像我想要的那样工作.
I'm making a java application and I need to play audio. I'm playing mainly small sound files of my cannon firing (its a cannon shooting game) and the projectiles exploding, though I plan on having looping background music. I have found two different methods to accomplish this, but both don't work how I want.
第一种方法实际上是一种方法:
The first method is literally a method:
public void playSoundFile(File file) {//http://java.ittoolbox.com/groups/technical-functional/java-l/sound-in-an-application-90681
try {
//get an AudioInputStream
AudioInputStream ais = AudioSystem.getAudioInputStream(file);
//get the AudioFormat for the AudioInputStream
AudioFormat audioformat = ais.getFormat();
System.out.println("Format: " + audioformat.toString());
System.out.println("Encoding: " + audioformat.getEncoding());
System.out.println("SampleRate:" + audioformat.getSampleRate());
System.out.println("SampleSizeInBits: " + audioformat.getSampleSizeInBits());
System.out.println("Channels: " + audioformat.getChannels());
System.out.println("FrameSize: " + audioformat.getFrameSize());
System.out.println("FrameRate: " + audioformat.getFrameRate());
System.out.println("BigEndian: " + audioformat.isBigEndian());
//ULAW format to PCM format conversion
if ((audioformat.getEncoding() == AudioFormat.Encoding.ULAW)
|| (audioformat.getEncoding() == AudioFormat.Encoding.ALAW)) {
AudioFormat newformat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
audioformat.getSampleRate(),
audioformat.getSampleSizeInBits() * 2,
audioformat.getChannels(),
audioformat.getFrameSize() * 2,
audioformat.getFrameRate(), true);
ais = AudioSystem.getAudioInputStream(newformat, ais);
audioformat = newformat;
}
//checking for a supported output line
DataLine.Info datalineinfo = new DataLine.Info(SourceDataLine.class, audioformat);
if (!AudioSystem.isLineSupported(datalineinfo)) {
//System.out.println("Line matching " + datalineinfo + " is not supported.");
} else {
//System.out.println("Line matching " + datalineinfo + " is supported.");
//opening the sound output line
SourceDataLine sourcedataline = (SourceDataLine) AudioSystem.getLine(datalineinfo);
sourcedataline.open(audioformat);
sourcedataline.start();
//Copy data from the input stream to the output data line
int framesizeinbytes = audioformat.getFrameSize();
int bufferlengthinframes = sourcedataline.getBufferSize() / 8;
int bufferlengthinbytes = bufferlengthinframes * framesizeinbytes;
byte[] sounddata = new byte[bufferlengthinbytes];
int numberofbytesread = 0;
while ((numberofbytesread = ais.read(sounddata)) != -1) {
int numberofbytesremaining = numberofbytesread;
sourcedataline.write(sounddata, 0, numberofbytesread);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
这个问题是我的整个程序停止直到声音文件完成,或者至少接近完成.
The problem with this is that my entire program stops until the sound file is finished, or at least nearly finished.
第二种方法是这样的:
File file = new File("Launch1.wav");
AudioClip clip;
try {
clip = JApplet.newAudioClip(file.toURL());
clip.play();
} catch (Exception e) {
e.getMessage();
}
我在这里遇到的问题是,每次声音文件提前结束或根本不播放取决于我放置代码的位置.
The problem I have here is that every time the sound file ends early or doesn't play at all depending on where I place the code.
他们有没有办法在没有上述问题的情况下播放声音?难道我做错了什么?非常感谢任何帮助.
Is their any way to play sound without the above mentioned problems? Am I doing something wrong? Any help is greatly appreciated.
推荐答案
对于第一种方法,您必须为音频创建另一个线程.
For the first method you have to create another thread for audio.
例如像这样:
new Thread(
new Runnable() {
public void run() {
try {
// PLAY AUDIO CODE
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
当然,您必须确保之前的声音不再播放.
Of course you have to make sure that previous sound isn't still playing.
这篇关于如何在 Java 应用程序中播放音频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!