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

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

    Hi!
    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 java.io.ByteArrayOutputStream;
    import java.io.IOException;
    
    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);
    			line.open(format);
    			line.start();
    			
    			Capture obj = new Capture(format,recordLine);
    			return obj.getByteArray();
    			
    		} catch (LineUnavailableException e) {
    			System.err.println("Line unavailable: " + e);
    			System.exit(-2);
    		}
    		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;
    		while(running){
    			try {
    				Thread.sleep(30);
    			} catch (InterruptedException e) {
    				e.printStackTrace();
    			}
    		}
    		return array;
    	}
    	
    	public void run() {
    		ByteArrayOutputStream out = new ByteArrayOutputStream();
    		running = true;
    		int max = 0;
    		try {
    			while (running) {
    				int count = line.read(buffer, 0, buffer.length);
    				
    				if (count > 0) {
    					System.out.println("Writing");
    					out.write(buffer, 0, count);
    				}
    				max++;
    				
    				if (max > 600000){
    					System.out.println("Stopping");
    				 running = false;
    				}
    			}
    			array = out.toByteArray();
    			out.close();
    		} catch (IOException e) {
    			System.err.println("I/O problems: " + e);
    			System.exit(-1);
    		}
    	}
    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,266
    Rep Power
    25

    Default

    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 com.sun.media.sound.PortMixer.getLine(PortMixer.ja va:120)
    at javax.sound.sampled.AudioSystem.getTargetDataLine( AudioSystem.java:731)

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

    Default

    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
  •