Results 1 to 3 of 3
  1. #1
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Rep Power

    Default My capture sound method returns empty byte array tho I have mic plugged in?

    Im trying to capture audio using a mic put in the mic input on my computer.
    So I modded a capture audio file that I found on the internet but for some
    reason it does not work for me?

    It doesnt return null, which meens it has been recordning.
    Tho all spots in the byte array returned is empty (null).

    Any idea how this is possible?
    Heres my code:
    PHP Code:
    import javax.sound.sampled.AudioFileFormat;
    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.Mixer;
    import javax.sound.sampled.Mixer.Info;
    import javax.sound.sampled.TargetDataLine;
    public class CaptureFrequency {
    	public static byte[] captureAudio() {
    		try {
    			Mixer.Info[] mixersInfo = AudioSystem.getMixerInfo();
    			//select virtual audiodevice by vendor name, device name or version
    			Mixer.Info selectedMixerInfo = mixersInfo[3];
    			TargetDataLine recordLine = AudioSystem.getTargetDataLine(getFormat(), selectedMixerInfo);
    			final AudioFormat format = getFormat();
    			DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
    			final TargetDataLine line = (TargetDataLine) AudioSystem.getLine(info);;
    			Capture obj = new Capture(format,recordLine);
    			return obj.getByteArray();
    		} catch (LineUnavailableException e) {
    			System.err.println("Line unavailable: " + e);
    		return null;
    	private static AudioFormat getFormat() {
    		float sampleRate = 8000.0f;
    		int sampleSizeInBits = 8;
    		int channels = 1;
    		boolean signed = true;
    		boolean bigEndian = true;
    		return new AudioFormat(sampleRate, sampleSizeInBits, channels, signed,bigEndian);
    class Capture implements Runnable{
    	int bufferSize = 0;
    	byte buffer[] = new byte[bufferSize];
    	private byte[] array;
    	private TargetDataLine line;
    	boolean running = false;
    	public Capture(AudioFormat format, TargetDataLine line){
    		bufferSize = (int) format.getSampleRate() * format.getFrameSize();
    		this.line = line;
    	public byte[] getByteArray(){
    		new Thread(this).start();
    		running = true;
    			try {
    			} catch (InterruptedException e) {
    		return array;
    	public void run() {
    		ByteArrayOutputStream out = new ByteArrayOutputStream();
    		running = true;
    		int max = 0;
    		try {
    			while (running) {
    				int count =, 0, buffer.length);
    				if (count > 0) {
    					out.write(buffer, 0, count);
    				if (max > 600000){
    				 running = false;
    			array = out.toByteArray();
    		} catch (IOException e) {
    			System.err.println("I/O problems: " + e);

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Eastern Florida
    Rep Power


    When I execute the code I get:
    java.lang.IllegalArgumentException: Line unsupported: interface TargetDataLine supporting format PCM_SIGNED 8000.0 Hz, 8 bit, mono, 1 bytes/frame,
    at va:120)
    at javax.sound.sampled.AudioSystem.getTargetDataLine(

  3. #3
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Rep Power


    Then it must be very dependent on what computer your using..
    I made a new code much smaller tho now it complains that the lines are not avalible..

Similar Threads

  1. Problems with method which returns result
    By new Object() in forum New To Java
    Replies: 2
    Last Post: 12-01-2010, 12:16 PM
  2. audio capture from sound card
    By kievari in forum Advanced Java
    Replies: 0
    Last Post: 11-03-2010, 07:00 PM
  3. Replies: 5
    Last Post: 10-30-2010, 12:05 AM
  4. How do I count empty spaces in a byte array?
    By nessa203 in forum New To Java
    Replies: 13
    Last Post: 01-11-2010, 05:46 PM
  5. Converting a byte[] into Sound
    By savage82 in forum Advanced Java
    Replies: 1
    Last Post: 11-21-2007, 11:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts