Java Sound refresh List of lines after connecting a microphone

I have a Swing capture / play application that should determine if there is a suitable microphone connected to the computer and alert the user. After searching many times, I found the only solution that allowed me to detect a newly attached or removed microphone:

     com.sun.media.sound.JDK13Services.setCachingPeriod(0);

     private static boolean isMicrophoneAvailable() {
        try {
            if (!AudioSystem.isLineSupported(Port.Info.MICROPHONE)) {
                log.debug("NO MICROPHONE FOUND");
                return false;
            } else {
                log.debug("MICROPHONE FOUND");
                return true;
            }
        } catch (IllegalArgumentException e) {
            log.debug("INCONSISTENT");
        }
        return false;
    }

called in the background thread as follows:

   new Thread() {
       public void run() {
            while(!thisFrame.isClosed()){
                if(isMicrophoneAvailable() == true){
                     //OK
                }else{
                     //WARN
                }
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
      }
    }).start();

The problem is that although the device was detected correctly using the described method, the list of base strings is not updated. That is, when the program starts and the device is connected later, the following exception occurs when trying to record sound:

 java.lang.IllegalArgumentException: No line matching interface TargetDataLine supporting format PCM_SIGNED 44100.0 Hz, 16 bit, stereo, 4 bytes/frame, big-endian is supported.

AudioSystem? , - JDK13Services, , ?

UPDATE: , :

        AudioFormat format = formatControls.getDefaultFormat();
        DataLine.Info info = new DataLine.Info(TargetDataLine.class,format);
        try {
            line = (TargetDataLine) AudioSystem.getLine(info);
            line.open(format, line.getBufferSize());
        } catch (LineUnavailableException ex) {
            shutDown("No audio input device available. Please make sure that a microphone is attached to your computer");
            return;
        } catch (Exception ex) {
            log.error(ex.toString());
            shutDown(ex.toString());
            return;
        }

:

 java.lang.IllegalArgumentException: No line matching interface TargetDataLine supporting format PCM_SIGNED 44100.0 Hz, 16 bit, stereo, 4 bytes/frame, big-endian is supported.
+5
2

, , .

, Port.Info , Dataline:

TargetDataLine dataLine = (TargetDataLine) AudioSystem.getLine(new DataLine.Info(TargetDataLine.class, audioFormat));                       
dataLine.open();
dataLine.start();               
dataLine.read(b, offset, len);

, , , Dataline , .

0

, , . ( ), USB- . . , , .

...
//call this once somewhere to turn the caching period down for faster detection
    try
    {
        //try to set the caching period, defaults to something like 55 seconds
        com.sun.media.sound.JDK13Services.setCachingPeriod(5);
    }
    catch( Exception e)
    {
        System.err.println("exception attempting to call com.sun.media.sound.JDK13Services.setCachingPeriod->" + e);
    }
...

private int lastNumMics = -1;
private synchronized void micCheck()
{
    Line.Info[] lineInfoArray = AudioSystem.getSourceLineInfo(Port.Info.MICROPHONE);
    int numMics = lineInfoArray == null ? 0 : lineInfoArray.length;
    if( lastNumMics > -1 )
    {
        if( numMics < lastNumMics )
        {
            //MICROPHONE_LOST
        }
        else if( numMics > lastNumMics )
        {
            //MICROPHONE_GAINED
        }
    }
    lastNumMics = numMics;
}
0

All Articles