I am trying to record sound through java, which is played on my computer using speakers / headphones.
The problem I'm stuck with is that I cannot find the only TargetDataLine that AudioSystem supports.
I tried the getSupportedFormats () method to check if there is any TargetDataLine that I can get, but I have 0 rows. If I change the argument in getSupportedFormats to SourceDataLine, I get 9 available rows.
Vector<AudioFormat> formats = getSupportedFormats(TargetDataLine.class);
System.out.println(formats.size());
public static Vector<AudioFormat> getSupportedFormats(Class<?> dataLineClass) {
float sampleRates[] = { (float) 8000.0, (float) 16000.0, (float) 44100.0 };
int channels[] = { 1, 2 };
int bytesPerSample[] = { 2 };
AudioFormat format;
DataLine.Info lineInfo;
Vector<AudioFormat> formats = new Vector<AudioFormat>();
for (Mixer.Info mixerInfo : AudioSystem.getMixerInfo()) {
for (int a = 0; a < sampleRates.length; a++) {
for (int b = 0; b < channels.length; b++) {
for (int c = 0; c < bytesPerSample.length; c++) {
format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
sampleRates[a], 8 * bytesPerSample[c], channels[b], bytesPerSample[c],
sampleRates[a], false);
lineInfo = new DataLine.Info(dataLineClass, format);
if (AudioSystem.isLineSupported(lineInfo)) {
if (AudioSystem.getMixer(mixerInfo).isLineSupported(lineInfo)) {
formats.add(format);
}
}
}
}
}
}
return formats;
}
I also tried most of the formats returned by the audioFormats method, which could not yet find the string.
public List<AudioFormat> audioFormats() throws LineUnavailableException{
Mixer.Info[] mi = AudioSystem.getMixerInfo();
List<AudioFormat> audioFormats = new ArrayList<AudioFormat>();
for (Mixer.Info info : mi) {
System.out.println("info: " + info);
Mixer m = AudioSystem.getMixer(info);
System.out.println("mixer " + m);
Line.Info[] sl = m.getSourceLineInfo();
for (Line.Info info2 : sl) {
System.out.println(" info: " + info2);
Line line = AudioSystem.getLine(info2);
if (line instanceof SourceDataLine) {
SourceDataLine source = (SourceDataLine) line;
DataLine.Info i = (DataLine.Info) source.getLineInfo();
for (AudioFormat format : i.getFormats()) {
audioFormats.add(format);
System.out.println(" format: " + format);
}
}
}
}
return audioFormats;
}
Here is an example of the class I tried
import javax.sound.sampled.*;
import javax.sound.sampled.AudioFormat.Encoding;
import java.io.*;
public class Recorder {
static final long RECORD_TIME = 30000;
File wavFile = new File("spacemusic.wav");
AudioFileFormat.Type fileType = AudioFileFormat.Type.WAV;
TargetDataLine line;
AudioFormat getAudioFormat() {
float sampleRate = 44100;
int sampleSizeInBits = 16;
int channels = 2;
boolean bigEndian = false;
Encoding encoding = Encoding.PCM_SIGNED;
int frameSize = 4;
float framRate = 44100;
AudioFormat format = new AudioFormat(encoding, sampleRate, sampleSizeInBits, channels, frameSize, framRate, bigEndian);
return format;
}
void start() throws UnsupportedAudioFileException {
try {
AudioFormat format = getAudioFormat();
DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
if (!AudioSystem.isLineSupported(info)) {
System.out.println("Line not supported");
System.exit(0);
}
line = (TargetDataLine) AudioSystem.getLine(info);
line.open(format);
line.start();
System.out.println("Start capturing...");
AudioInputStream ais = new AudioInputStream(line);
System.out.println("Start recording...");
AudioSystem.write(ais, fileType, wavFile);
} catch (LineUnavailableException ex) {
ex.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
void finish() {
line.stop();
line.close();
System.out.println("Finished");
}
public static void main(String[] args) throws UnsupportedAudioFileException {
final Recorder recorder = new Recorder();
Thread stopper = new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(RECORD_TIME);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
recorder.finish();
}
});
stopper.start();
recorder.start();
}
}
Any idea on how to get a supported string so that I can continue recording. Thanks