我正在尝试在程序中添加声音。我没有经验如何在程序中添加声音。 我正在尝试这个,但这段代码给了我异常“null”。我不知道我要在playSound函数中传递什么参数。请帮助我。
import java.io.InputStream;
import sun.audio.*; //import the sun.audio package
import java.io.*;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
public class Main {
public Main() {
}
public static void main(String[] args) {
System.out.println("hello there");
playSound("Hi there");
}
public static synchronized void playSound(final String url) {
new Thread(new Runnable() { // the wrapper thread is unnecessary, unless
// it blocks on the Clip finishing, see
// comments
public void run() {
try {
Clip clip = AudioSystem.getClip();
AudioInputStream inputStream = AudioSystem
.getAudioInputStream(Main.class
.getResourceAsStream("pacman_chomp.wav"
+ url));
clip.open(inputStream);
clip.start();
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
}).start();
}
}
答案 0 :(得分:1)
它可能会给你一个null异常,因为它无法找到该文件。目前它正在寻找一个名为:
的文件“pacman_chomp.wavHi那里”
因为url的值被设置为“Hi there”而你正在构建它:
AudioInputStream inputStream = AudioSystem
.getAudioInputStream(Main.class
.getResourceAsStream("pacman_chomp.wav"
+ url));
我认为你想要文件“”pacman_chomp.wav“?在这种情况下删除”+ url“。
答案 1 :(得分:1)
既然你说你的playSound方法不起作用,这里是我编码的声音类。
import java.io.IOException;
import java.io.InputStream;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.UnsupportedAudioFileException;
public class AePlayWave extends Thread {
protected static final boolean DEBUG = false;
protected InputStream inputStream;
public AePlayWave(InputStream inputStream) {
this.inputStream = inputStream;
if (DEBUG) System.out.println("AePlayWave constructor");
}
@Override
public void run() {
if (DEBUG) System.out.println("AePlayWave running");
AudioInputStream audioInputStream = verifyInputStream();
if (audioInputStream == null) {
return;
}
AudioFormat format = audioInputStream.getFormat();
SourceDataLine audioLine = openInputStream(format);
if (DEBUG) System.out.println(audioLine.getLineInfo());
if (audioLine != null) {
audioLine.start();
playInputStream(audioInputStream, audioLine);
}
}
protected AudioInputStream verifyInputStream() {
if (DEBUG) System.out.println("AePlayWave verifyInputStream");
AudioInputStream audioInputStream = null;
try {
audioInputStream = AudioSystem.getAudioInputStream(inputStream);
} catch (UnsupportedAudioFileException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
return audioInputStream;
}
protected SourceDataLine openInputStream(AudioFormat format) {
if (DEBUG) System.out.println("AePlayWave openInputStream");
DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
SourceDataLine audioLine = null;
if (DEBUG) System.out.println("AePlayWave openInputStream try");
try {
audioLine = (SourceDataLine) AudioSystem.getLine(info);
if (DEBUG) System.out.println("AePlayWave openInputStream getLine");
audioLine.open(format);
if (DEBUG) System.out.println("AePlayWave openInputStream open");
} catch (LineUnavailableException e) {
e.printStackTrace();
return null;
} catch (Exception e) {
e.printStackTrace();
return null;
}
return audioLine;
}
protected void playInputStream(AudioInputStream audioInputStream,
SourceDataLine audioLine) {
if (DEBUG) System.out.println("AePlayWave playInputStream");
int externalBufferSize = (int) audioInputStream.getFrameLength() * 4;
if (DEBUG) System.out.println("AePlayWave playInputStream externalBufferSize: "
+ externalBufferSize);
int nBytesRead = 0;
byte[] abData = new byte[externalBufferSize];
try {
while (nBytesRead != -1) {
nBytesRead = audioInputStream.read(abData, 0, abData.length);
if (nBytesRead >= 0) {
if (DEBUG) System.out.println("Bytes read: " + nBytesRead);
audioLine.write(abData, 0, nBytesRead);
}
}
} catch (IOException e) {
e.printStackTrace();
return;
} finally {
audioLine.drain();
audioLine.close();
}
}
}
以下是它的名称:
InputStream inputStream = getClass().getResourceAsStream("alarm-clock-1.wav");
AePlayWave playWave = new AePlayWave(inputStream);
playWave.run();
答案 2 :(得分:0)
Clip不允许长音频文件。请参阅我的游戏引擎中的这些类。
然后使用此代码。
// First load the sound
WavSound snd = WavPlayer.loadSound("pacman_chomp.wav");
// play it then
snd.play();
您可以免费使用这些课程。希望他们有所帮助。
答案 3 :(得分:0)
playSound("pacman_chomp.wav");
public static synchronized void playSound(final String url) {
new Thread(new Runnable() { // the wrapper thread is unnecessary, unless
// it blocks on the Clip finishing, see
// comments
public void run() {
try {
Clip clip = AudioSystem.getClip();
AudioInputStream inputStream = AudioSystem
.getAudioInputStream(Main.class
.getResourceAsStream(url));
clip.open(inputStream);
clip.start();
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
}).start();
}
a:如上所述,它会查找“pacman_chomp.wavHi there”
b:此处不建议使用硬编码。你失去了重用代码的能力。例如,如果您在playSound方法中进行硬编码,则无法执行此操作:
playSound("pacman_chomp.wav");
playSound("pacman_dies.wav");
此外,方法名称“playSound”名称不佳(在硬编码文件中),因为它将始终播放pacman_chomp文件(如果正确指出)。
c:无论如何,“你好”的目的是什么?