如何在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();
}
我这里的问题是,每次声音文件提前结束或不玩都取决于我在哪里放置code。
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.
时他们没有办法播放声音没有上述问题?难道我做错了什么?任何帮助是极大的AP preciated。
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();
当然,你必须确保previous声音不还在打。
Of course you have to make sure that previous sound isn't still playing.
这篇关于如何在Java应用程序中播放音频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!