Reputation: 167
I already can get the bytestream of the operation system standard input device. I now want to change the input device while the program is already running.
That is the code to get the targetDataLine/input stream of the standard input device (it works):
dataLineInfo = new DataLine.Info( TargetDataLine.class , getAudioFormat() ) ;
targetDataLine = (TargetDataLine)AudioSystem.getLine( dataLineInfo ) ;
targetDataLine.open( getAudioFormat() );
targetDataLine.start();
I also can get the list of all available input devices and give them out in a combobox to select another input device (it works):
Mixer.Info[] mixerInfo;
mixerInfo = AudioSystem.getMixerInfo();
Line.Info targetDLInfo = new Line.Info(TargetDataLine.class);
for(int cnt = 0; cnt < mixerInfo.length; cnt++) {
Mixer currentMixer = AudioSystem.getMixer(mixerInfo[cnt]);
if( currentMixer.isLineSupported(targetDLInfo) ) {
combo1.addItem(mixerInfo[cnt].getName());
}
}
That is the code how I change the input device (it doesn't work)
if(e.getSource() == combo1){
System.out.println("Gewählter Input: " + combo1.getSelectedItem());
Mixer.Info[] mixerInfo;
mixerInfo = AudioSystem.getMixerInfo();
Line.Info targetDLInfo = new Line.Info(TargetDataLine.class);
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");
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();
}
}}
}
In fact I think I only made a small mistake with the last part but I don't know what mistake. If I switch to another audio input device I just hear a small beep and then nothing. If I switch back to the primary audio device I can hear the input again. I don't get any errors.
What did I do wrong?
Upvotes: 1
Views: 1741
Reputation: 11
On the line with the following code
if( mixerInfo[cnt].getName() == combo1.getSelectedItem().toString()) {
You've not compared the strings correctly. When comparing strings use the String.equals(String)
method, like this:
if( mixerInfo[cnt].getName().equals(combo1.getSelectedItem().toString())) {
If you use the ==
operator on any two non-primitive objects, only the pointers of the two objects will be compared. String objects will in the vast majority of cases have different pointer values, like any other dynamically allocated object, regardless if the locations they point to contain the same arrangement of bytes.
Upvotes: 1