JAVA音频流输入函数示例

2018-08-22  本文已影响9人  艾回
import java.io.*;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;//需要引入的包

class Shengyin extends Thread{
    private String wjm;
    public Shengyin(String ypwj) {
        wjm=ypwj;
    }
    public void run() {
        File wjl=new File(wjm);
        AudioInputStream ypsrl=null;
        try {
            ypsrl=AudioSystem.getAudioInputStream(wjl);//音频输入流
        }catch(Exception e) {}
        AudioFormat format=ypsrl.getFormat();
        SourceDataLine aqsj=null;
        DataLine.Info info=new DataLine.Info(SourceDataLine.class, format);
        try {
            aqsj=(SourceDataLine)AudioSystem.getLine(info);
            aqsj.open(format);
        }catch(Exception e) {}
        aqsj.start();
        int zjtj=0;
        byte[] hczj=new byte[1024];//缓冲字节
        try {
            while(zjtj!=-1) {
                zjtj=ypsrl.read(hczj,0,hczj.length);
                if(zjtj>=0)
                    aqsj.write(hczj,0,zjtj);
            }
        }catch(Exception e) {}
        finally {
            aqsj.drain();//将残留部分处理干净
            aqsj.close();
        }
    }
}
上一篇下一篇

猜你喜欢

热点阅读