import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.filechooser.FileFilter;


/** *//**
 * @author Administrator
 *
 */
public class test1 extends JFrame {
    /** *//**
     *
     */
    private AudioInputStream ais;
    private String fileUrl="WindowsDing.wav";

    private class MyFilter  extends FileFilter {

        @Override
        public boolean accept(File arg0) {
            // TODO Auto-generated method stub
            if(arg0.getName().endsWith(".wav" )|| arg0.isDirectory())
                return true;
            return false;
        }

        @Override
        public String getDescription() {
            // TODO Auto-generated method stub
            return "聲音檔案(.wav)";
        }
        
    }
    
    
    test1() {
        super();
        setSize(150,150);
        JButton startBut=new JButton("播放");
        startBut.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                JFileChooser jfc= new JFileChooser();
                jfc.setFileFilter(new MyFilter());
                if(jfc.showOpenDialog(null)==JFileChooser.APPROVE_OPTION)
                    fileUrl=jfc.getSelectedFile().getAbsolutePath();
                else
                    return;
                
                try {
                    ais=AudioSystem.getAudioInputStream(new File(fileUrl));
                    AudioFormat af=ais.getFormat();
                    SourceDataLine sdl=null;
                    DataLine.Info info=new DataLine.Info(SourceDataLine.class,af);
                    sdl=(SourceDataLine) AudioSystem.getLine(info);
                    sdl.open(af);
                    sdl.start();
                    
                    //play
                    
                    int nByte=0;
                    byte[] buffer=new byte[128];
                    while(nByte!=-1) {
                        nByte=ais.read(buffer,0,128);
                        if(nByte>=0) {
                            int oByte=sdl.write(buffer, 0, nByte);
                            System.out.println(oByte);
                        }
                    }
                    sdl.stop();
                    
                } catch (UnsupportedAudioFileException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (LineUnavailableException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                
            }
            
        });
        this.getContentPane().add(startBut);
        setVisible(true);
    }
    
    public static void main(String[] args) {
        new test1();
    }
}

台南小新 發表在 痞客邦 PIXNET 留言(0) 人氣()