JAVA中,我的音乐文件在同一目录下,为什么放不出音乐呢,谢谢大家了

importjava.awt.*;importjava.awt.event.*;importjava.applet.*;importjavax.swing.*;impor... import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import java.net.*;

public class SoundTest1 extends JApplet {

private static final long serialVersionUID = 1L;
boolean isStandalone = false;
String sound;
JButton jButtonPlay = new JButton();
JButton jButtonLoop = new JButton();
JButton jButtonStop = new JButton();
JLabel jLabel1 = new JLabel();
AudioClip clip;

// 读取参数
public String getParameter(String key, String def) {
return isStandalone ? System.getProperty(key, def)
: (getParameter(key) != null ? getParameter(key) : def);
}

// 初始化Applet
public void init() {
try {
jbInit();
} catch (Exception e) {
e.printStackTrace();
}
}

// 初始化组件
private void jbInit() throws Exception {
jButtonPlay.setText("Play");
jButtonPlay.setBounds(new Rectangle(50, 85, 80, 40));
jButtonPlay.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(MouseEvent e) {
jButtonPlay_mouseClicked(e);
}
});
this.setSize(new Dimension(400, 200));
this.getContentPane().setLayout(null);
jButtonLoop.setText("Loop");
jButtonLoop.setBounds(new Rectangle(150, 85, 80, 40));
jButtonLoop.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(MouseEvent e) {
jButtonLoop_mouseClicked(e);
}
});
jButtonStop.setText("Stop");
jButtonStop.setBounds(new Rectangle(250, 85, 80, 40));
jButtonStop.addMouseListener(new java.awt.event.MouseAdapter() {

public void mouseClicked(MouseEvent e) {
jButtonStop_mouseClicked(e);
}
});
jLabel1.setText("java 声音播放测试");
jLabel1.setBounds(new Rectangle(109, 28, 186, 28));
this.getContentPane().add(jButtonPlay, null);
this.getContentPane().add(jButtonStop, null);
this.getContentPane().add(jButtonLoop, null);
this.getContentPane().add(jLabel1, null);
try {
sound = this.getParameter(clip.toString(), "12.wav");
} catch (Exception e) {
e.printStackTrace();
}
if (sound != null) {
try {
clip = JApplet.newAudioClip(new URL(getCodeBase(), sound));

} catch (MalformedURLException e) {
System.out.println("Bad URL");
}
}
}

void jButtonPlay_mouseClicked(MouseEvent e) {
clip.play();
}

void jButtonLoop_mouseClicked(MouseEvent e) {
clip.loop();
}

void jButtonStop_mouseClicked(MouseEvent e) {
clip.stop();
}
}
展开
 我来答
Rankabc
2014-11-20 · TA获得超过3561个赞
知道大有可为答主
回答量:3705
采纳率:59%
帮助的人:1076万
展开全部
.wav用完整路径试试

如果是用Eclipse的话,和src在同一目录下,而不是在src里面
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式