2013-03-18 29 views
0

我試圖將播放方法放在我的Audioapp類中,位於我的ActionListener中,我嘗試將其放入,但出現錯誤。試圖在我的ActionListener中放置播放音頻方法

我看過的javadoc上ActionListener但無法找到它

這是我的聲音:

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

我是初學者所以它很可能是一個非常愚蠢的基本錯誤

任何幫助將不勝感激

+1

1)你能得到血腥的代碼格式嗎?!? 2)*「但是得到了錯誤。」*喜歡什麼?複製/粘貼這些東西,不要讓我們猜測。 3)爲了更快得到更好的幫助,請發佈[SSCCE](http://sscce.org/)。 4)爲什麼這是一個applet?他們是一個PITA來開發,並且對於新手來說太過先進。 – 2013-03-18 16:04:58

+0

「歌曲無法解析」 – dhali 2013-03-18 16:07:58

+2

在您的動作監聽器中,在if語句中,( ==「東西」)沒有任何意義。嘗試使用.equals()方法,String.equals(「Things」)。 – Nixon 2013-03-18 16:18:49

回答

0

song是私人。您創建了一種播放聲音的方法,以便使用它。

獲取Audioapp的實例並運行playSound()方法。

1
songPath = new URL(getCodeBase(),"G:\\Uni\\Programming\\Rolling assignements\\Week0\\Programming week21"); 

URL構造函數期望第二個字符串表示一個相對URL,而您已經放置了一個文件路徑。網址總是有正斜槓,而基於file:的網址則毫無意義(WAV託管在您的網站上,而不是最終用戶的HD)。

如果小程序是由HTML在沒有在HTML中聲明codebase網站的根目錄加載,而WAV被稱爲moo.wav,位於名爲Week0/Programming week21子目錄正確的路徑將是:

songPath = new URL(getCodeBase(),"Week0/Programming week21/moo.wav"); 

但爲了讓事情簡單了很多,至少就目前而言,把HTML,小應用程序同一目錄夾及用途:

songPath = new URL(getCodeBase(),"moo.wav");