0

play メソッドを 内のAudioappクラス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);
        }
    }

私は初心者なので、おそらく非常に愚かな基本的なエラーです

どんな助けでもいただければ幸いです

4

2 に答える 2

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

URL コンストラクターは、2 番目の文字列が相対 URL を表していることを期待していますが、ファイル パスを指定しています。URL には常にスラッシュがあり、file:そこに基づく URL はほとんど意味がありません (エンド ユーザーの HD ではなく、サイトで WAV がホストされます)。

codebaseHTML で宣言されて いないサイトのルートで、アプレットが HTML によって読み込まれ、wav が呼び出され、名前がmoo.wav付けられたサブディレクトリに配置されWeek0/Programming week21ている場合、正しいパスは次のようになります。

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

しかし、物事をもっと簡単にするために、少なくとも今のところ、HTML、アプレット、およびクリップを同じディレクトリに置き、次を使用します。

songPath = new URL(getCodeBase(),"moo.wav");
于 2013-03-18T16:16:29.690 に答える
0

songプライベートです。サウンドを再生するメソッドを作成したので、それを使用します。

あなたのインスタンスを取得しAudioapp、メソッドを実行しますplaySound()

于 2013-03-18T16:07:36.587 に答える