我正在编写一个键盘小程序,但按下按钮时无法播放声音文件。没有出现错误,我已经尝试将文件转换为.au,但它仍然没有播放。请帮忙。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.applet.*;
public class Keyboard extends Applet
implements ActionListener
{
Button menu;
Button[] black;
Button[] white;
AudioClip[] bnote;
AudioClip[] wnote;
public void init ()
{
setLayout (null);
setBackground (Color.orange);
menu = new Button ("Back to Menu");
menu.setBounds (20, 200, 100, 30);
add (menu);
black = new Button [5];
int x = 45;
for (int i = 0 ; i < 5 ; i++)
{
if (i == 2 || i == 5 || i == 7 || i == 10)
{
x = x + 40;
}
black [i] = new Button ();
black [i].setBounds (x, 20, 30, 90);
black [i].setBackground (Color.black);
add (black [i]);
black [i].addActionListener (this);
x = x + 40;
}
white = new Button [7];
x = 20;
for (int i = 0 ; i < 7 ; i++)
{
white [i] = new Button ();
white [i].setBounds (x, 20, 40, 150);
white [i].setBackground (Color.white);
add (white [i]);
white [i].addActionListener (this);
x = x + 40;
}
wnote = new AudioClip [7];
wnote [0] = getAudioClip (getCodeBase (), "C.wav");
wnote [1] = getAudioClip (getCodeBase (), "D.wav");
wnote [2] = getAudioClip (getCodeBase (), "E.wav");
wnote [3] = getAudioClip (getCodeBase (), "F.wav");
wnote [4] = getAudioClip (getCodeBase (), "G.wav");
wnote [5] = getAudioClip (getCodeBase (), "A.wav");
wnote [6] = getAudioClip (getCodeBase (), "B.wav");
bnote = new AudioClip [5];
bnote [0] = getAudioClip (getCodeBase (), "Db.wav");
bnote [1] = getAudioClip (getCodeBase (), "Eb.wav");
bnote [2] = getAudioClip (getCodeBase (), "F#.wav");
bnote [3] = getAudioClip (getCodeBase (), "Ab.wav");
bnote [4] = getAudioClip (getCodeBase (), "Bb.wav");
}
public void actionPerformed (ActionEvent evt)
{
int num = 0;
for (int i = 0 ; i < 7 ; i++)
{
if (evt.getSource () == white [i])
{
wnote [i].play ();
}
}
for (int i = 0 ; i < 5 ; i++)
{
if (evt.getSource () == black [i])
{
bnote [i].play ();
}
}
}
}