利用Applet中的AudioClip播放声音class SoundThread implements Runnable{
private AudioClip m = null;
private URL url = null;
private boolean isRun = true; public SoundThread() {
url = this.getClass().getResource("/folder.wav");
try {
m = Applet.newAudioClip(url);
} catch (Exception e) {
e.printStackTrace();
}
} public void stop(){
isRun = false;
m.stop();
} public void run() {
long time = System.currentTimeMillis();
long lasttime = time + 3 * 1000;
while (time < lasttime && isRun) {
m.play();
time = System.currentTimeMillis();
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
}
}
}
public static void main(String[] args){
SoundThread st = new SoundThread();
new Thread(st).start();
}
}
private AudioClip m = null;
private URL url = null;
private boolean isRun = true; public SoundThread() {
url = this.getClass().getResource("/folder.wav");
try {
m = Applet.newAudioClip(url);
} catch (Exception e) {
e.printStackTrace();
}
} public void stop(){
isRun = false;
m.stop();
} public void run() {
long time = System.currentTimeMillis();
long lasttime = time + 3 * 1000;
while (time < lasttime && isRun) {
m.play();
time = System.currentTimeMillis();
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
}
}
}
public static void main(String[] args){
SoundThread st = new SoundThread();
new Thread(st).start();
}
}
解决方案 »
- 哪位大侠帮我看一下这个“文件传输”代码(文件长度穿不过去)
- 高手帮忙怎么能把MyFrame放到Container中去,以便于设置背景!
- override the method in superclass/interface
- 用Struts+spring+Hibernate写一个用户登陆权限?
- 如何令窗体启动时自动执行某个单击事件?
- 各位高手:
- 数据库连接时出现:...Error eatablishing socket错误.
- 急!!!如何从一个webapp里得到另一个webapp中的session中的数据???
- 我很想知道java能够做些什么软件???
- 我在JB3。5里面MAKE我的Cabin远程接口,怎么会出这个错?
- java日期操作
- JAVA混合编程
《Swing Hacks》,
public void paint(Graphics g)
{
int cw = getWidth();
int ch = getHeight();
URL url = getClass().getResource("about.jpg");
ImageIcon scaledSymbol = new ImageIcon(new ImageIcon(url).getImage()
.getScaledInstance(cw, ch, Image.SCALE_SMOOTH));
g.drawImage(scaledSymbol.getImage(), 0, 0, this);
}
声音
public void mousePressed(MouseEvent e)
{ URL url = this.getClass().getResource("beep.au");
try
{
FileInputStream fileau = new FileInputStream(
new File(url.getFile()));
AudioStream as = new AudioStream(fileau);
AudioPlayer.player.start(as);
}
catch (Exception ex)
{
ex.printStackTrace();
}
.......
}能凑合着用,不是很完美。
JLabel myImg = new JLabel(new ImageIcon(this.getClass().getResource("URL")));
至于添加背景音乐,在Eclipse中好像不行,需要另外下载包.