Java Sound refresh Lines list after attaching a mi

2020-03-20 17:28发布

I have a simple capture/playback Swing app that has to detect if there is no appropriate microphone attached to the computer and warn the user. After a lot of fiddling around I have found the only solution that allowed me to detect the 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 like this:

   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 is being detected correctly using the described method, the list of underlying Lines is not refreshed. That is, when the program is started, and the device is attached later, the following exception is thrown 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.

Is there any way to get the lines list of AudioSystem refreshed? Maybe something similar to the JDK13Services workaround used at the very beginning to avoid caching?

UPDATE: Code that throws an exception:

        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;
        }

and the Exception itself:

 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.

2条回答
虎瘦雄心在
2楼-- · 2020-03-20 17:36

Posting the code that throws the exception might help.

I'm assuming you are only using the Port.Info to detect the presence of a microphone then getting a Dataline to record:

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

Note that you might still get a similar exception when disconnecting the microphone if the microphone is physically disconnected between the time you check for presence and the time you try to get the Dataline to record but connecting the microhpone should not be a problem.

查看更多
家丑人穷心不美
3楼-- · 2020-03-20 17:58

Using the original post as inspiration, I came up with this as a means to detect when a mic is lost or gained. It detects (on my system) when a USB mic is plugged in or unplugged. I'm calling it from a background thread loop. The original method did not work for me as there is a built-in mic on the laptop, so I needed to detect the addition of second mic.

...
//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;
}
查看更多
登录 后发表回答