I have a Runnable that gets a line to the Mic, reads from it and stores it in an OutputStream.
When I start the 1st instance of this thread it works.
After 1st thread completes (exists run() method) i start another instance of the thread but this time i get:
javax.sound.sampled.LineUnavailableException: line with format PCM_SIGNED 44100.0 Hz, 8 bit, stereo, 2 bytes/frame, not supported.
at com.sun.media.sound.DirectAudioDevice$DirectDL.implOpen(Unknown Source)
at com.sun.media.sound.AbstractDataLine.open(Unknown Source)
at com.sun.media.sound.AbstractDataLine.open(Unknown Source)
My thread looks like this:
public void run() {
float sampleRate = 44100.00F;
//8000,11025,16000,22050,44100
int sampleSizeInBits = 8;
//8,16
int channels = 2;
//1,2
boolean signed = true;
//true,false
boolean bigEndian = false;
//true,false
AudioFormat format = new AudioFormat(sampleRate,
sampleSizeInBits,
channels,
signed,
bigEndian);
DataLine.Info info = new DataLine.Info(TargetDataLine.class, format); // format is an AudioFormat object
// Obtain and open the line.
try {
targetLine = (TargetDataLine) AudioSystem.getLine(info);
targetLine.open(format); //exception is throw here
targetLine.start();
ByteArrayOutputStream out = new ByteArrayOutputStream();
CountingOutputStream countingOutStream = new CountingOutputStream(out);
int numBytesRead;
byte[] data = new byte[targetLine.getBufferSize() / 5];
File binFile = new File("C:\\audio\\random4.txt");
FileOutputStream fr = new FileOutputStream(binFile);
while ( (countingOutStream.getCount()/1024) < targetSizeKB) {
numBytesRead = targetLine.read(data, 0, data.length);
countingOutStream.write(data, 0, numBytesRead);
}
fr.write(newByte,0,ii);
countingOutStream.close();
fr.close();
targetLine.flush();
targetLine.stop();
targetLine = null;
}
You’re calling
flushandstopontargetLine, but you don’t callclose– I suspect you need to do that in order to release it properly. (It’s not clear whytargetLineis an instance variable, by the way – does it need to be?)You should also be doing all the clean-up in
finallyblocks, so that the streams get closed even if an exception is thrown.