问题
I initialize the AudioInputStream
like this:
dataLineInfo = new DataLine.Info(TargetDataLine.class, getAudioFormat()) ;
targetDataLine = (TargetDataLine) AudioSystem.getLine(dataLineInfo);
targetDataLine.open(getAudioFormat());
targetDataLine.start();
That works fine and I can hear the Audio Input of the Microphone.
If I try to change the Audio Input to another device I only hear noise. I tried to solve this problem for a week now and I really don't have a clue anymore why I can't hear the other audio input device... I would be very proud about any help!
Mixer.Info[] mixerInfo = AudioSystem.getMixerInfo();
for (int cnt = 0; cnt < mixerInfo.length; cnt++) {
Mixer currentMixer = AudioSystem.getMixer(mixerInfo[cnt]);
if (mixerInfo[cnt].getName() == combo1.getSelectedItem().toString()) {
System.out.println("Gewählter Input gefunden");
if (targetDataLine.isRunning()) {
targetDataLine.stop();
}
targetDataLine.flush();
if (targetDataLine.isOpen()) {
targetDataLine.close();
}
dataLineInfo = new DataLine.Info(TargetDataLine.class, getAudioFormat());
try {
targetDataLine = (TargetDataLine) currentMixer.getLine(dataLineInfo);
targetDataLine.open(getAudioFormat());
targetDataLine.start();
} catch(LineUnavailableException e1) {
e1.printStackTrace();
}
}
}
For complete code, go to github
回答1:
I am certain the problem is in the actionPerformed()
method:
dataLineInfo = new DataLine.Info( TargetDataLine.class , getAudioFormat() ) ;
try {
targetDataLine = (TargetDataLine) currentMixer.getLine(dataLineInfo) ;
targetDataLine.open(getAudioFormat());
targetDataLine.start();
// TODO: You must read what's in the buffer here
} catch (LineUnavailableException e1) {
e1.printStackTrace();
}
When the Microphone2
object is first created, this part is invoked:
public void run(){
while(continueLoop){
n = targetDataLine.read(tempBuffer, 0, tempBuffer.length);
updateMeter();
try {
Object_Output_Stream.writeObject(tempBuffer);
Object_Output_Stream.reset();
} catch (IOException ex) {
ex.printStackTrace();
continueLoop = false;
}
}
}
As you can see, the buffer is read and the output stream is filled with what was in the buffer. This does not happen when the actionPerformed()
is invoked.
来源:https://stackoverflow.com/questions/26828282/why-do-i-get-only-hear-noise-after-changing-the-audio-input-in-java