我一直试图用java播放声音而且我被卡住了。
public class Audioapp extends JApplet
{
public class Sound{
public AudioClip song;
public URL songPath;
Sound(){
try{
songPath = new URL(getCodeBase(), "leftfordeath.wav");
song = Applet.newAudioClip(songPath);
}
catch (Exception e) {}
}
public void playSoundOnce(){
song.play();
}
}
public void init(){
Sound test = new Sound();
test.playSoundOnce();
}
}
当我从main调用init时,它说song.play()上有一个nullPointerExeption; 我做错了什么,请帮忙......
答案 0 :(得分:1)
尝试将此代码替换为您拥有的playoundOnce代码并查看如何继续使用。
public void playSoundOnce()
{
try
{
InputStream inputStream = getClass().getResourceAsStream(leftfordeath.wav);
AudioStream audioStream = new AudioStream(inputStream);
AudioPlayer.player.start(audioStream);
}
catch (Exception e)
{
if (debugFileWriter!=null) e.printStackTrace(debugFileWriter);
}
}