簡體   English   中英

試圖在我的ActionListener中放置一個播放音頻方法

[英]trying to put a play audio method in my ActionListener

我試圖將play方法放在ActionListener Audioapp類中,但嘗試出現錯誤。

我已經在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);
        }
    }

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

任何幫助,將不勝感激

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

URL構造函數希望第二個字符串表示相對URL,而您已經放置了文件路徑。 URL始終帶有正斜杠和一個file:基於URL的意義不大(WAV托管在您的站點上,而不是最終用戶的HD)。

如果小程序是在站點的根目錄中由HTML加載的,而HTML中沒有聲明codebase ,並且wav稱為moo.wav且位於名為Week0/Programming week21則正確的路徑為:

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

但是,為了使事情變得簡單得多,至少就目前而言,將HTML,applet 剪輯放在同一目錄中並使用:

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

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM