import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class Player extends JApplet{
AudioClip currentaudio;
JButton openaudio,playaudio,loopaudio,stopaudio;
public void init(){
Container container =getContentPane();
container.setLayout(new FlowLayout());
ButtonHandler handler=new ButtonHandler();
openaudio=new JButton("打开");
playaudio=new JButton("播放");
loopaudio=new JButton("循环");
stopaudio=new JButton("停止");
openaudio.addActionListener(handler);
playaudio.addActionListener(handler);
loopaudio.addActionListener(handler);
stopaudio.addActionListener(handler);
container.add(openaudio);
container.add(playaudio);
container.add(loopaudio);
container.add(stopaudio);
}
public void stop(){
currentaudio.stop();
}
class choiceListener implements ItemListener{
public void itemStateChanged(ItemEvent e){
currentaudio.stop();
}
}
private class ButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent e){
if(e.getSource()==openaudio)
JFileChooser chooser=new JFileChooser();
chooser.addChoosableFileFilter(new MusicFile());
int option=chooser.showOpenDialog(this);
if(option==javax.swing.JFileChooser.APPROVE_OPTION){
java.io.File file=chooser.getSelectedFile();
if(file==null) return;
String str=new String();
str=chooser.getSelectedFile().getName();
list.add(str);
try{
FileInputStream input=new FileInputStream(
chooser.getSelectedFile().getPath());
FileOutputStream output=new FileOutputStream(
"C://eclipse" +
"//workspace//lc0506401_19//"+
"hi_hit.mid");
int in=input.read();
while(in!=-1){
output.write(in);
in=input.read();
}
}catch (IOException t){
System.out.println(t.toString());
}
System.out.print(chooser.getSelectedFile().getName());
audio=getAudioClip(getDocumentBase(),
"1.mid");
}
else if(e.getSource()==playaudio)
currentaudio.play();
else if(e.getSource()==loopaudio)
currentaudio.loop();
else if(e.getSource()==stopaudio)
currentaudio.stop();
}
}
private static class MusicFile extends javax.swing.filechooser.FileFilter{
public boolean accept(java.io.File file){
if(file==null)
return false;
return file.isDirectory() ||
file.getName().toLowerCase().endsWith(".mid")||
file.getName().toLowerCase().endsWith(".wav")||
file.getName().toLowerCase().endsWith(".au");
}
public String getDescription(){
return "Music files(*.mid,*.mav,*.au)";
}
}
}