我正在尝试将播放方法放在我Audioapp
内的ActionListener
课程中,我尝试将其放入,但却出错了。
我已在ActionListener
上阅读了javadoc但找不到它
这是我的音频:
public class Audioapp extends JApplet // find out how to implement play method into action listener?
{
public class Sound // Holds one audio file
{
private AudioClip song; // Sound player
private URL songPath; // Sound path
Sound(String filename)
{
try
{
songPath = new URL(getCodeBase(),"G:\\Uni\\Programming\\Rolling assignements\\Week0\\Programming week21"); // This is the place were im getting error
song = Applet.newAudioClip(songPath); // Load the Sound
}
catch(Exception e){e.printStackTrace();}
}
public void playSound()
{
song.play(); // Play
}
}
}
这是我的actionListener
:
class PlayStoHandler implements ActionListener {
public void actionPerformed(ActionEvent event) {
String computerRand = sps.computerChoice();
txtComputerRand.setText(computerRand);
String result = sps.play(Sps.ROCK);
txtResult.setText(result);
scis.setVisible(false);
open.setVisible(false);
stone.setVisible(true);
pap.setVisible(false);
win.setVisible(false);
none.setVisible(false);
lose.setVisible(false);
if (result == "User Wins"){
win.setVisible(true);
song.play(); // here i tried putting the song.play in this if section, the error I got was "song cannot be resolved"
}
else if (result == "Draw"){
none.setVisible(true);
}
else {
lose.setVisible(true);
}
}
我是初学者,所以这很可能是一个非常愚蠢的基本错误
任何帮助将不胜感激
答案 0 :(得分:1)
songPath = new URL(getCodeBase(),"G:\\Uni\\Programming\\Rolling assignements\\Week0\\Programming week21");
URL构造函数需要第二个字符串表示相对URL,而您已放置文件路径。网址总是有正斜杠,基于file:
的网址没什么意义(WAV托管在您的网站上,而不是最终用户的HD)。
如果applet正在HTML网站的根目录下加载,而HTML中没有声明codebase
,则wav名为moo.wav
,位于名为{{的子目录中1}}正确的路径是:
Week0/Programming week21
但为了让事情变得更简单,至少在目前,将HTML,applet 和剪辑放在 同一目录 并使用:
songPath = new URL(getCodeBase(),"Week0/Programming week21/moo.wav");
答案 1 :(得分:0)
song
是私有的。您创建了一种播放声音的方法,因此请使用它。
获取Audioapp
的实例并运行playSound()
方法。