package SST.com.tw;

import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sound.sampled.*;

public class RecorderAudio extends Thread {
    static TargetDataLine m_targetdataline;
    static AudioFileFormat.Type m_targetType;
    static AudioInputStream m_audioInputStream;

    static File m_outputFile;
    static ByteArrayOutputStream bos = new ByteArrayOutputStream();
    static byte[] buf;
    static boolean m_bRecording;

    public RecorderAudio(TargetDataLine line, AudioFileFormat.Type targetType,
                        File file) {
        m_targetdataline = line;
        m_audioInputStream = new AudioInputStream(line);
        m_targetType = targetType;
        m_outputFile = file;
    }
    public static void AudioRecorder() {
        String Filename = "d://aa.wav";
        File outputFile = new File(Filename);

        AudioFormat audioFormat = null;

        audioFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 8000.0F,
                                      8, 1, 1, 8000.0F, false);
        // telephone sample rate is 8KHz
        DataLine.Info info = new DataLine.Info(TargetDataLine.class, audioFormat);
        System.out.println(info.toString());
        TargetDataLine targetDataLine = null;
        
        try {
            targetDataLine = (TargetDataLine) AudioSystem.getLine(info);
            targetDataLine.open(audioFormat);
        } catch (LineUnavailableException e) {
            System.out.println("audio record fail");
            System.exit(-1);
        }
        AudioFileFormat.Type targetType = AudioFileFormat.Type.AU;
        RecorderAudio recorder = null;

        recorder = new RecorderAudio(targetDataLine, targetType, outputFile);
        recorder.start();
        try {
            Thread.sleep(3000);
        } catch (InterruptedException ex) {
            System.out.println(ex.toString());
        }
        recorder.stopRecording();

        try {
            FileOutputStream fos = new FileOutputStream(m_outputFile);
            fos.write(buf);
            System.out.println("file write");
            fos.close();
        } catch (Exception e) {
            System.out.println("file write");
        }
    }
    public void start() {
        m_targetdataline.start();
        super.start();
        System.out.println("recording...");
    }
    public static void stopRecording() {
        m_targetdataline.stop();
        m_targetdataline.close();
        m_bRecording = false;
        buf = bos.toByteArray();
        System.out.println("stopped");
    }
    public void run() {
        try {
            AudioSystem.write(m_audioInputStream, m_targetType, bos);
            System.out.println("after write()");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 ffyy99 的頭像
    ffyy99

    喜歡亂搞的世界

    ffyy99 發表在 痞客邦 留言(0) 人氣()