我正在尝试通过java录制声音,该声音正在我的Windows机器上通过扬声器/耳机播放。
我遇到的问题是我没有找到 AudioSystem 支持的单个 TargetDataLine。
我尝试了 getSupportedFormats() 方法来检查是否有任何可以获取的 TargetDataLine,但是我得到了 0 行。如果将 getSupportedFormats 中的参数更改为 SourceDataLine,我将获得 9 个可用行。
Vector<AudioFormat> formats = getSupportedFormats(TargetDataLine.class);
System.out.println(formats.size());
public static Vector<AudioFormat> getSupportedFormats(Class<?> dataLineClass) {
/*
* These define our criteria when searching for formats supported
* by Mixers on the system.
*/
float sampleRates[] = { (float) 8000.0, (float) 16000.0, (float) 44100.0 };
int channels[] = { 1, 2 };
int bytesPerSample[] = { 2 };
AudioFormat format;
DataLine.Info lineInfo;
//SystemAudioProfile profile = new SystemAudioProfile(); // Used for allocating MixerDetails below.
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)) {
/*
* TODO: To perform an exhaustive search on supported lines, we should open
* TODO: each Mixer and get the supported lines. Do this if this approach
* TODO: doesn't give decent results. For the moment, we just work with whatever
* TODO: the unopened mixers tell us.
*/
if (AudioSystem.getMixer(mixerInfo).isLineSupported(lineInfo)) {
formats.add(format);
}
}
}
}
}
}
return formats;
}
另外,我已经尝试了 audioFormats 方法返回的大多数格式,但仍然无法找到一行。
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;
}
这是我尝试过的示例类
import javax.sound.sampled.*;
import javax.sound.sampled.AudioFormat.Encoding;
import java.io.*;
public class Recorder {
// record duration, in milliseconds
static final long RECORD_TIME = 30000; // 1 minute
// path of the wav file
File wavFile = new File("spacemusic.wav");
// format of audio file
AudioFileFormat.Type fileType = AudioFileFormat.Type.WAV;
// the line from which audio data is captured
TargetDataLine line;
/**
* Defines an audio format
*/
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(sampleRate, sampleSizeInBits,
channels, signed, bigEndian);*/
AudioFormat format = new AudioFormat(encoding, sampleRate, sampleSizeInBits, channels, frameSize, framRate, bigEndian);
return format;
}
/**
* Captures the sound and record into a WAV file
* @throws UnsupportedAudioFileException
*/
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(); // start capturing
System.out.println("Start capturing...");
AudioInputStream ais = new AudioInputStream(line);
System.out.println("Start recording...");
// start recording
AudioSystem.write(ais, fileType, wavFile);
} catch (LineUnavailableException ex) {
ex.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
/**
* Closes the target data line to finish capturing and recording
*/
void finish() {
line.stop();
line.close();
System.out.println("Finished");
}
/**
* Entry to run the program
* @throws UnsupportedAudioFileException
*/
public static void main(String[] args) throws UnsupportedAudioFileException {
final Recorder recorder = new Recorder();
// creates a new thread that waits for a specified
// of time before stopping
Thread stopper = new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(RECORD_TIME);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
recorder.finish();
}
});
stopper.start();
// start recording
recorder.start();
}
}
关于如何获得受支持的线路以便我可以继续录制的任何想法。谢谢