java调用本地声音问题!帮帮忙!
package welcome;
import java.applet.Applet;
import java.applet.AudioClip;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class aaa extends JFrame implements ActionListener{
private JButton jb1;
private AudioClip loopClip;
private JButton jb2;
private JFrame jf;
public aaa(){
init();
}
public void actionPerformed(ActionEvent e){
}
public void init(){
sound();
jf = new JFrame( "Audio ");
jf.setSize(300,200);
jb1 = new JButton( "start ");
jb2 = new JButton( "stop ");
jb1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
startsound();
}
});
jb2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
stopsound();
}
});
jb1.setBounds(60,50,60,60);
jb2.setBounds(90,120,60,60);
jf.getContentPane().setLayout(null);
jf.getContentPane().add(jb1);
jf.getContentPane().add(jb2);
jf.show();
}
public void sound()
{
String str = "file:F:\\zhuce\\love.wav ";
try{
loopClip=Applet.newAudioClip(new URL(str));
}catch(Exception e){
System.out.println(e);
}
}
public final void startsound(){
loopClip.play();
}
public final void stopsound(){
loopClip.stop();
}
public static void main(String[] args){
new aaa();
}
}
没有异常,但是也播放不了!麻烦大家帮忙看看!
------解决方案--------------------public void sound()
{
try{
loopClip=getAudioClip(getCodeBase(), "love.wav ");
}catch(Exception e){
System.out.println(e);
}
}
//将love.wav文件放到类文件夹中,或welcome目录中