0
votes

When i press the button, netbeans itself says: "Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: No line matching interface TargetDataLine supporting format PCM_SIGNED 44100.0 Hz, 16 bit, mono, 2 bytes/frame, big-endian is supported." When the line is not supported, it should pop up an error message saying "line is not supported". Instead, nothing happens. What should i do?

public class Ouvir extends NewJFrame{

AudioFormat audioFormat;
TargetDataLine targetDataLine;
TargetDataLine line;

void captureAudio(){

          Listen.setEnabled(false);
          try{
          audioFormat = getAudioFormat();
          DataLine.Info info = new DataLine.Info(TargetDataLine.class, audioFormat);
          line = (TargetDataLine) AudioSystem.getLine(info);  
          AudioSystem.getLine(info);

          if (!AudioSystem.isLineSupported(info)) {
              String error = "Line not supported";
              JOptionPane.showMessageDialog(null,error,"+",JOptionPane.ERROR_MESSAGE);
              line.close();
          }

          line.open();
          line.start();
         }
          catch (LineUnavailableException e) {}
       }

void stopCapture(){ 

    if(line != null)
       {
       line.stop();
       line.close();
       }
    if(!Stop.getModel().isPressed())
       {
       line.stop();
       line.close();
       }
       }

private AudioFormat getAudioFormat(){


       return new AudioFormat(44100,16,1,true,true);
  }
  }
1
You get the AudioLine before you test if it's supported - MadProgrammer
how do i solve this? didn't quite get - Gabriel Mendes
You need to call AudioSystem.isLineSupported(info) before AudioSystem.getLine(info), otherwise, how do you know if it can be supported - MadProgrammer

1 Answers

1
votes

Basically what you're doing is trying to get a AudioLine before checking to see if it's possible

AudioSystem.getLine(info);
if (!AudioSystem.isLineSupported(info)) {...

getLine is throwing the unsupported exception because you called it first. You need to reverse your logic

if (AudioSystem.isLineSupported(info)) {
    AudioSystem.getLine(info);
} else {
    // Show error
}