Results 1 to 1 of 1
  1. #1
    B_Tank88 is offline Member
    Join Date
    Jul 2011
    Posts
    22
    Rep Power
    0

    Default Audiosystem.write not returning?

    Hello,

    I am doing a task which involves screenshot and sound capture, but I am having problems with AudioSystem.write, in run(). The method doesn't seem to return whether I have a mic plugged in or not, and a blank .wav file seems to be written. Can someone help?

    Here is my output:

    1. Entered main function
    2. Entered Capture class
    Writing file: 2012-01-31_11-56-47.jpg
    Write Successful: 2012-01-31_11-56-47.jpg
    3. Entered main_soundcapture
    4. Audio/file formats initialised
    5. TargetDataline grabbed
    Press ENTER to start the recording.

    Started recording
    Try audiosystem.write


    Please ignore the block comments, I don't know what happened there.

    Java Code:
    import java.io.File;
    import java.io.IOException;
    import java.text.DateFormat;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    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.TargetDataLine;
    
    public class SoundCapture {
    	
    	TargetDataLine		m_line;
    	AudioFileFormat.Type	m_targetType;
    	AudioInputStream	m_audioInputStream;
    	File			m_outputFile;
    	DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
    	
    	public void main_soundcapture() {
    		
    		System.out.println("3. Entered main_soundcapture");
    		
    /*		initialise the audio data format; */
    	    Date date = new Date();
    		String strFilename = dateFormat.format(date);
    		File	outputFile = new File(strFilename + ".wav");
    		
    		AudioFormat	audioFormat = new AudioFormat(
    				AudioFormat.Encoding.PCM_SIGNED,
    				44100.0F, 16, 2, 4, 44100.0F, false);
    		
    /*		initialise audio file type;*/
    		AudioFileFormat.Type	targetType = AudioFileFormat.Type.WAVE;
    		
    		System.out.println("4. Audio/file formats initialised");
    		
    /*		initialise new target data line, if requesting the line was successful then open the target data line;*/	
    		
    		DataLine.Info	info = new DataLine.Info(TargetDataLine.class, audioFormat);
    		TargetDataLine	targetDataLine = null;
    		try
    		{
    			targetDataLine = (TargetDataLine) AudioSystem.getLine(info);
    			targetDataLine.open(audioFormat);
    		}
    		catch (LineUnavailableException e)
    		{
    			out("unable to get a recording line");
    			e.printStackTrace();
    			System.exit(1);
    		}
    		
    		System.out.println("5. TargetDataline grabbed");
    		
    		m_line = targetDataLine;
    		m_audioInputStream = new AudioInputStream(targetDataLine);
    		m_targetType = targetType;
    		m_outputFile = outputFile;
    		
    		out("Press ENTER to start the recording.");
    		try
    		{
    			System.in.read();
    		}
    		catch (IOException e)
    		{
    			e.printStackTrace();
    		}
    		
    		start();
    		out("Recording...");
    		
    		out("Press ENTER to stop the recording.");
    		try
    		{
    			System.in.read();
    		}
    		catch (IOException e)
    		{
    			e.printStackTrace();
    		}
    
    		/* Here, the recording is actually stopped.
    		 */
    		stopRecording();
    		out("Recording stopped.");
    		
    	}
    	
    	private static void out(String strMessage)
    	{
    		System.out.println(strMessage);
    	}
    	
    	public void start()
    	{
    		System.out.println("Started recording");
    		m_line.start();
    		run();
    	}
    	
    	public void stopRecording()
    	{
    		m_line.stop();
    		m_line.close();
    	}
    	
    	// CRASHES HERE
    	
    	public void run()
    	{
    			try
    			{
    				System.out.println("Try audiosystem.write");
    				AudioSystem.write(
    					m_audioInputStream,
    					m_targetType,
    					m_outputFile);
    			}
    			catch (IOException e)
    			{
    				System.out.println("Catch IOException");
    				e.printStackTrace();
    			}
    			System.out.println("Recording complete");
    	}
    }
    Last edited by B_Tank88; 01-31-2012 at 01:10 PM.

Similar Threads

  1. Replies: 7
    Last Post: 11-30-2011, 06:58 PM
  2. help returning a min value
    By Meta in forum New To Java
    Replies: 7
    Last Post: 04-11-2011, 12:28 AM
  3. Accessing AudioSystem getMixerInfo()??
    By Jlister76 in forum New To Java
    Replies: 6
    Last Post: 02-25-2011, 12:04 PM
  4. Help with returning
    By Da1dmoney in forum Eclipse
    Replies: 3
    Last Post: 12-15-2010, 02:38 AM
  5. Replies: 3
    Last Post: 03-03-2010, 09:51 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
  •