gpt4 book ai didi

java - AudioClip 不起作用,但它应该

转载 作者:行者123 更新时间:2023-12-01 11:59:07 25 4
gpt4 key购买 nike

我一直在尝试在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;我做错了什么,请帮忙...

最佳答案

尝试将此代码替换为 PlaysoundOnce 代码,看看效果如何。

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);
}
}

关于java - AudioClip 不起作用,但它应该,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28119944/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com