Sunday, July 13, 2014

Audio Record Using Java

package com.pkm.sound.record;

import java.io.File;

public class AudioRecorder {
    private static final int RECORD_TIME = 6 * 1000;   // 6 seconds
    
    public static void main(String[] args) {
        File wavFile = new File("Record.wav");
        final SoundRecordingUtil recorder = new SoundRecordingUtil();
        Thread recordThread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    System.out.println("Start recording...");
                    recorder.start();
                } catch (Exception ex) {
                    ex.printStackTrace();
                    System.exit(-1);
                }              
            }
        });
         
        recordThread.start();
         
        try {
            Thread.sleep(RECORD_TIME);
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
         
        try {
            recorder.stop();
            recorder.save(wavFile);
            System.out.println("STOPPED");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
         
        System.out.println("DONE");        
    }
}

No comments:

Post a Comment