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();
}
} 展开
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();
}
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |