能通过编译,但点播放后就是没声音出来
import java.net.*;
import java.applet.*;
import java.awt.event.*;
import java.awt.Container;
import java.awt.FlowLayout;
import javax.swing.*;
//播放音频片断的实例
public class SoundTest extends JFrame implements ActionListener{
private JButton playMusic=new JButton("Play Music");
private JButton loopMusic=new JButton("Loop Music");
private JButton stopMusic=new JButton("stop Music");
private JButton playSound=new JButton("Play Sound");
private JButton stopSound=new JButton("Stop Sound");
private AudioClip music=null,sound=null;
public SoundTest(){
super("Sound Test");
try{
String separator=System.getProperty("file.separator");
String preface="file:"+System.getProperty("user.dir")+separator+"Audio"+separator;
music=Applet.newAudioClip(new URL(preface+"music.mid"));
sound=Applet.newAudioClip(new URL(preface+"applause.mid"));
}
catch(MalformedURLException murle){
System.err.println("Error loading files:"+murle);
}
Container content=getContentPane();
content.setLayout(new FlowLayout());
content.add(playMusic);playMusic.addActionListener(this);
content.add(loopMusic);loopMusic.addActionListener(this);
content.add(stopMusic);stopMusic.addActionListener(this);
content.add(playSound);playSound.addActionListener(this);
content.add(stopSound);stopSound.addActionListener(this);
validate();pack();setVisible(true);
}
public void actionPerformed(ActionEvent ae){
if(ae.getSource()==playMusic)
music.play();
else if(ae.getSource()==loopMusic)
music.loop();
else if(ae.getSource()==stopMusic)
music.stop();
else if(ae.getSource()==playSound)
sound.play();
else if(ae.getSource()==stopSound)
sound.stop();
}
public static void main(String args[]){
SoundTest st=new SoundTest();
}
}